2. Agenda
• What is Behavioral Design Pattern?
• Observer
• Mediator
• State
• Strategy
• Iterator
• Visitor
3. 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!
4. 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!
7. 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.
25. 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.
38. 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.
39. 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;
}
}
}
40. 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;
}
}
}
42. 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
44. 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.
45.
46.
47. 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.
49. 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.
56. 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.
59. 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) { ... }
60. Internal vs. External Iterator
• Internal- iteration controlled by the
iterator itself.
• External- client controls iteration
by requesting the next element.
66. 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.
68. 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!