SlideShare a Scribd company logo
1 of 70
Download to read offline
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
Behavioral Design Patterns

More Related Content

What's hot

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagrams
APU
 

What's hot (20)

PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design Patterns
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Software architecture and software design
Software architecture and software designSoftware architecture and software design
Software architecture and software design
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
react redux.pdf
react redux.pdfreact redux.pdf
react redux.pdf
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagrams
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the Domain
 
Psychology of usable things
Psychology of usable thingsPsychology of usable things
Psychology of usable things
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
 
Software Engineering - chp4- design patterns
Software Engineering - chp4- design patternsSoftware Engineering - chp4- design patterns
Software Engineering - chp4- design patterns
 
Java exception
Java exception Java exception
Java exception
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Android share preferences
Android share preferencesAndroid share preferences
Android share preferences
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Testing Spring Applications
Testing Spring ApplicationsTesting Spring Applications
Testing Spring Applications
 
Visitor Pattern
Visitor PatternVisitor Pattern
Visitor Pattern
 

Viewers also liked

Designing and using group software through patterns
Designing and using group software through patternsDesigning and using group software through patterns
Designing and using group software through patterns
Kyle Mathews
 
Sofwear deasign and need of design pattern
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design pattern
chetankane
 
Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621
melbournepatterns
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
Dang Tuan
 
Customer Relationship Management
Customer Relationship ManagementCustomer Relationship Management
Customer Relationship Management
Dr. Praveen Pillai
 

Viewers also liked (16)

Designing and using group software through patterns
Designing and using group software through patternsDesigning and using group software through patterns
Designing and using group software through patterns
 
Sofwear deasign and need of design pattern
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design pattern
 
Iterator Pattern
Iterator PatternIterator Pattern
Iterator Pattern
 
Iterator
IteratorIterator
Iterator
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
Organizational behavior chapter 8 team
Organizational behavior chapter 8 teamOrganizational behavior chapter 8 team
Organizational behavior chapter 8 team
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
 
Customer Relationship Management
Customer Relationship ManagementCustomer Relationship Management
Customer Relationship Management
 
Customer Relationship Management
Customer Relationship ManagementCustomer Relationship Management
Customer Relationship Management
 
Customer relationship management
Customer relationship managementCustomer relationship management
Customer relationship management
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
 
The Basics Of CRM
The Basics Of CRMThe Basics Of CRM
The Basics Of CRM
 
Customer Relationship Management (CRM)
Customer Relationship Management (CRM)Customer Relationship Management (CRM)
Customer Relationship Management (CRM)
 

Similar to Behavioral Design Patterns

Observer Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 AugObserver Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 Aug
melbournepatterns
 
ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013
Mathias Seguy
 
Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development
Mahmoud Hamed Mahmoud
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
indiaartz
 

Similar to Behavioral Design Patterns (20)

Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonf
 
Demystifying Reactive Programming
Demystifying Reactive ProgrammingDemystifying Reactive Programming
Demystifying Reactive Programming
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
An Introduction To CQRS
An Introduction To CQRSAn Introduction To CQRS
An Introduction To CQRS
 
N Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeN Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React Native
 
Observer Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 AugObserver Pattern Khali Young 2006 Aug
Observer Pattern Khali Young 2006 Aug
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Mobile Application Development class 006
Mobile Application Development class 006Mobile Application Development class 006
Mobile Application Development class 006
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Tool Development 07 - Undo & Redo, Drag & Drop
Tool Development 07 - Undo & Redo, Drag & DropTool Development 07 - Undo & Redo, Drag & Drop
Tool Development 07 - Undo & Redo, Drag & Drop
 
ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013
 
Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
 
lecture10-patterns.ppt
lecture10-patterns.pptlecture10-patterns.ppt
lecture10-patterns.ppt
 
lecture10-patterns.ppt
lecture10-patterns.pptlecture10-patterns.ppt
lecture10-patterns.ppt
 
Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development Windows Store app using XAML and C#: Enterprise Product Development
Windows Store app using XAML and C#: Enterprise Product Development
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 

Recently uploaded

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 

Recently uploaded (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 

Behavioral Design Patterns