Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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!

Slide 4

Slide 4 text

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!

Slide 5

Slide 5 text

Last Summer

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

Solution #1: Busy-wait

Slide 9

Slide 9 text

Solution #1: Busy-wait

Slide 10

Slide 10 text

Solution #2: Notification through composition

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Solution using Observer Pattern

Slide 14

Slide 14 text

Observer

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Observer Demo

Slide 18

Slide 18 text

Oref (HFC) Devices

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Monitoring- response time, errors App servers Autoscaling Logging

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Logging Monitoring- response time, errors App servers Autoscaling

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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.

Slide 26

Slide 26 text

Mediator- Structure

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Reusability

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Responsibility

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

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; } } }

Slide 40

Slide 40 text

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; } } }

Slide 41

Slide 41 text

Solution using State Pattern

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

State Pattern- Structure

Slide 44

Slide 44 text

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.

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

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.

Slide 48

Slide 48 text

Strategy- Structure

Slide 49

Slide 49 text

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.

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

State Changing who changes the state?

Slide 52

Slide 52 text

Encapsulation

Slide 53

Slide 53 text

When to use

Slide 54

Slide 54 text

Problem

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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.

Slide 57

Slide 57 text

Iterator- Structure

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Java Iterator List arr = new ArrayList(); 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) { ... }

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Complex data structures

Slide 62

Slide 62 text

Elements in the collection may be removed

Slide 63

Slide 63 text

Visitor

Slide 64

Slide 64 text

Problem

Slide 65

Slide 65 text

Solution using Visitors

Slide 66

Slide 66 text

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.

Slide 67

Slide 67 text

Structure

Slide 68

Slide 68 text

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!

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

No content