Behavioral Design Patterns

2,954 views 70 slides Dec 23, 2014
Slide 1
Slide 1 of 70
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70

About This Presentation

Behavioral Design Patterns
Foundations of Software Engineering, Ben Gurion University, Fall 2015

Observer, Mediator, State, Strategy, Iterator, Visitor


Slide Content

Behavioral
Design Patterns
Lidan Hifi
Sagi Avasker
Amit Bedarshi
Foundations of software engineering, Fall 2015

Agenda
•What is Behavioral Design Pattern?
•Observer
•Mediator
•State
•Strategy
•Iterator
•Visitor

Behavioral Patterns
•Defines the communication between objects.
•Dynamic behavior (changeable in runtime)
using polymorphism.
•Objects are able to talk each other, and still
loosely coupled!

Behavioral Patterns
•Defines the communication between objects.
•Dynamic behavior (changeable in runtime)
using polymorphism.
•Objects are able to talk each other, and still
loosely coupled!

Last Summer

Motivation
Define a one-to-many dependency
between objects, so that when one
object changes its state, all its
dependents are notified and 

updated automatically.

Solution #1: Busy-wait

Solution #1: Busy-wait

Solution #2:
Notification through composition

Solution #2:
Notification through composition
for (Recipient x : recipients) {
if (x instanceof Android) {
sendAndroid(x, msg);
} else if (x instanceof IOS) {
sendiOS(x, msg);
}
...
}

Solution #2:
Notification through composition
for (Recipient x : recipients) {
if (x instanceof Android) {
sendAndroid(x, msg);
} else if (x instanceof IOS) {
sendiOS(x, msg);
}
...
}

Solution using Observer Pattern

Observer

Examples
•Event Listeners
•C# Delegates (register a function, and invoke it
later)

Observer Demo

Oref (HFC) Devices

Monitoring- response time,
errors
App
servers
Autoscaling
Logging

Logging
Monitoring- response time,
errors
App
servers
Autoscaling

Logging
Monitoring- response time,
errors
Application
Servers
(Mediator)

Mediator Pattern
•Define an object that encapsulates how a
set of objects interact.
•Mediator promotes 

loose coupling by 

keeping objects from 

referring to each other 

explicitly.

Mediator- Structure

Relationship
One-to-many
vs.
Many-to-many

Reusability

Responsibility

Possible Solution
•Use enum that represents the current
state / screen (watching, VOD menu,
EPG, etc.)
•Switch-case statement each time the
user clicks on a multi-state button.

enum State { CHANNEL_INFO, TV, VOD, EPG, MENU, GAMES }
class DigitalTVRemote {
State m_state;
public void menuButton() { ... }
public void infoButton() { ... }
public void exitButton() {
switch (m_state) {
case MENU:
m_state = TV;
showChannel();
break;
case VOD:
backButton();
break;
case CHANNEL_INFO:
m_state = TV;
hideChannelInfo();
break;
}
}
}

enum State { CHANNEL_INFO, TV, VOD, EPG, MENU, GAMES }
class DigitalTVRemote {
State m_state;
public void menuButton() { ... }
public void infoButton() { ... }
public void exitButton() {
switch (m_state) {
case MENU:
m_state = TV;
showChannel();
break;
case VOD:
backButton();
break;
case CHANNEL_INFO:
m_state = TV;
hideChannelInfo();
break;
}
}
}

Solution using State Pattern

State Pattern- Motivation
•An object-oriented State Machine.
•Allow an object to alter its behavior at
runtime when its internal state changes.
The object will appear to change its class

State Pattern- Structure

State- Pros & Cons
Pros:
•Provides an easy way to change the behavior of a
given object in runtime, based on its current state.
•Adding a new state is very easy.
Cons:
•Many classes which are not part of the system
design are added.

Strategy- Motivation
•Defines a family of algorithms, encapsulate
each one and make then interchangeable.
•Lets the algorithm vary independently from
the client that use it.
•Choose the preferred algorithm to solving the
problem, according to the current situation.

Strategy- Structure

Strategy- Pros & Cons
Pros:
•Provides an easy way to change the behavior of a
given object in runtime, based on the current situation.
•Adding a new algorithm is very easy.
Cons:
•Client must be aware of a different strategies.
•Creates many classes which are not part of the system
design directly.

State
Changing
who changes the state?

Encapsulation

When
to use

Problem

Problem
collection.sort();
collection.merge(c2);
collection.find(x);

Iterator- Motivation
•Provide a way to access the elements of an
aggregate object sequentially without
exposing its underlying implementation.
•Provides a uniform interface for traversing
different kinds of collections.

Iterator- Structure

Java Iterator
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}

Java Iterator
List<Integer> arr = new ArrayList<Integer>();
Iterator it = arr.iterator();
Integer i = it.next();
// for loop using iterator
for (Iterator it = arr.iterator(); 

it.hasNext(); it.next()) { ... }
// Syntactic suger (foreach loop)
for (Integer i : arr) { ... }

Internal vs. External Iterator
•Internal- iteration controlled by the
iterator itself.
•External- client controls iteration
by requesting the next element.

Complex data
structures

Elements in the collection
may be removed

Visitor

Problem

Solution using Visitors

Motivation
•Represent an operation to be performed on the
elements of an object structure.
•Visitor lets you define a new operation, without
changing the classes of the elements on which
it operates.

Structure

Visitor- Pros & Cons
Pros:
•Easy to add more services: just add a visitor
class.
Cons:
•Hard to add a new class to the original
hierarchy- need to change all the visitors!

Summary
•Observer
•Mediator
•State
•Strategy
•Iterator
•Visitor