behavioral design patterns

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

Upload: lidan-hifi

Post on 16-Jul-2015

166 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Behavioral Design Patterns

Behavioral Design Patterns

Lidan Hifi Sagi Avasker Amit Bedarshi

Foundations of software engineering, Fall 2015

Page 2: Behavioral Design Patterns

Agenda• What is Behavioral Design Pattern?

• Observer

• Mediator

• State

• Strategy

• Iterator

• Visitor

Page 3: Behavioral Design Patterns

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!

Page 4: Behavioral Design Patterns

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!

Page 5: Behavioral Design Patterns

Last Summer

Page 6: Behavioral Design Patterns
Page 7: Behavioral Design Patterns

MotivationDefine a one-to-many dependency between objects, so that when one object changes its state, all its dependents are notified and updated automatically.

Page 8: Behavioral Design Patterns

Solution #1: Busy-wait

Page 9: Behavioral Design Patterns

Solution #1: Busy-wait

Page 10: Behavioral Design Patterns

Solution #2: Notification through composition

Page 11: Behavioral Design Patterns

Solution #2: Notification through composition

for (Recipient x : recipients) { if (x instanceof Android) { sendAndroid(x, msg); } else if (x instanceof IOS) { sendiOS(x, msg); }

...}

Page 12: Behavioral Design Patterns

Solution #2: Notification through composition

for (Recipient x : recipients) { if (x instanceof Android) { sendAndroid(x, msg); } else if (x instanceof IOS) { sendiOS(x, msg); }

...}

Page 13: Behavioral Design Patterns

Solution using Observer Pattern

Page 14: Behavioral Design Patterns

Observer

Page 15: Behavioral Design Patterns

Examples

• Event Listeners

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

Page 16: Behavioral Design Patterns
Page 17: Behavioral Design Patterns

Observer Demo

Page 18: Behavioral Design Patterns

Oref (HFC) Devices

Page 19: Behavioral Design Patterns
Page 20: Behavioral Design Patterns

Monitoring- response time,errors

Appservers

Autoscaling

Logging

Page 21: Behavioral Design Patterns
Page 22: Behavioral Design Patterns

Logging Monitoring- response time,errors

Appservers

Autoscaling

Page 23: Behavioral Design Patterns
Page 24: Behavioral Design Patterns

Logging Monitoring- response time,errors

Application Servers(Mediator)

Page 25: Behavioral Design Patterns

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.

Page 26: Behavioral Design Patterns

Mediator- Structure

Page 27: Behavioral Design Patterns
Page 28: Behavioral Design Patterns

RelationshipOne-to-many

vs. Many-to-many

Page 29: Behavioral Design Patterns

Reusability

Page 30: Behavioral Design Patterns
Page 31: Behavioral Design Patterns

Responsibility

Page 32: Behavioral Design Patterns
Page 33: Behavioral Design Patterns
Page 34: Behavioral Design Patterns
Page 35: Behavioral Design Patterns
Page 36: Behavioral Design Patterns
Page 37: Behavioral Design Patterns
Page 38: Behavioral Design Patterns

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.

Page 39: Behavioral Design Patterns

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

Page 40: Behavioral Design Patterns

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

Page 41: Behavioral Design Patterns

Solution using State Pattern

Page 42: Behavioral Design Patterns

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

Page 43: Behavioral Design Patterns

State Pattern- Structure

Page 44: Behavioral Design Patterns

State- Pros & ConsPros:

• 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.

Page 45: Behavioral Design Patterns
Page 46: Behavioral Design Patterns
Page 47: Behavioral Design Patterns

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.

Page 48: Behavioral Design Patterns

Strategy- Structure

Page 49: Behavioral Design Patterns

Strategy- Pros & ConsPros:

• 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.

Page 50: Behavioral Design Patterns
Page 51: Behavioral Design Patterns

StateChanging

who changes the state?

Page 52: Behavioral Design Patterns

Encapsulation

Page 53: Behavioral Design Patterns

When to use

Page 54: Behavioral Design Patterns

Problem

Page 55: Behavioral Design Patterns

Problem

collection.sort();

collection.merge(c2);

collection.find(x);

Page 56: Behavioral Design Patterns

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.

Page 57: Behavioral Design Patterns

Iterator- Structure

Page 58: Behavioral Design Patterns

Java Iterator

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

Page 59: Behavioral Design Patterns

Java IteratorList<Integer> arr = new ArrayList<Integer>();Iterator it = arr.iterator();Integer i = it.next();

// for loop using iteratorfor (Iterator it = arr.iterator(); it.hasNext(); it.next()) { ... }

// Syntactic suger (foreach loop)for (Integer i : arr) { ... }

Page 60: Behavioral Design Patterns

Internal vs. External Iterator

• Internal- iteration controlled by the iterator itself.

• External- client controls iteration by requesting the next element.

Page 61: Behavioral Design Patterns

Complex data structures

Page 62: Behavioral Design Patterns

Elements in the collection may be removed

Page 63: Behavioral Design Patterns

Visitor

Page 64: Behavioral Design Patterns

Problem

Page 65: Behavioral Design Patterns

Solution using Visitors

Page 66: Behavioral Design Patterns

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.

Page 67: Behavioral Design Patterns

Structure

Page 68: Behavioral Design Patterns

Visitor- Pros & ConsPros:

• 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!

Page 69: Behavioral Design Patterns

Summary• Observer

• Mediator

• State

• Strategy

• Iterator

• Visitor

Page 70: Behavioral Design Patterns