Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Behavioral Design Patterns

Lidan
December 23, 2014

Behavioral Design Patterns

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

Observer, Mediator, State, Strategy, Iterator, Visitor

Lidan

December 23, 2014
Tweet

More Decks by Lidan

Other Decks in Programming

Transcript

  1. Agenda • What is Behavioral Design Pattern? • Observer •

    Mediator • State • Strategy • Iterator • Visitor
  2. 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!
  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. 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.
  5. Solution #2: Notification through composition for (Recipient x : recipients)

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

    { if (x instanceof Android) { sendAndroid(x, msg); } else if (x instanceof IOS) { sendiOS(x, msg); } ... }
  7. 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.
  8. 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.
  9. 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; } } }
  10. 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; } } }
  11. 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
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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) { ... }
  17. Internal vs. External Iterator • Internal- iteration controlled by the

    iterator itself. • External- client controls iteration by requesting the next element.
  18. 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.
  19. 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!