SlideShare una empresa de Scribd logo
1 de 70
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
Haitham El-Ghareeb
 
Requirements elicitation
Requirements elicitationRequirements elicitation
Requirements elicitation
Abdul Basit
 
Ch6-Software Engineering 9
Ch6-Software Engineering 9Ch6-Software Engineering 9
Ch6-Software Engineering 9
Ian Sommerville
 
Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
Slideshare
 

La actualidad más candente (20)

Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
UML Diagrams
UML DiagramsUML Diagrams
UML Diagrams
 
An Introduction to Software Architecture
An Introduction to Software ArchitectureAn Introduction to Software Architecture
An Introduction to Software Architecture
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
Software design
Software designSoftware design
Software design
 
Agile Process models
Agile Process modelsAgile Process models
Agile Process models
 
Software requirements
Software requirementsSoftware requirements
Software requirements
 
Requirements elicitation
Requirements elicitationRequirements elicitation
Requirements elicitation
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Ch5 system modeling
Ch5 system modelingCh5 system modeling
Ch5 system modeling
 
Ch6-Software Engineering 9
Ch6-Software Engineering 9Ch6-Software Engineering 9
Ch6-Software Engineering 9
 
Software Architecture Patterns
Software Architecture PatternsSoftware Architecture Patterns
Software Architecture Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Use Case Diagram
Use Case DiagramUse Case Diagram
Use Case Diagram
 
Gathering requirements
Gathering requirementsGathering requirements
Gathering requirements
 
Command Design Pattern
Command Design Pattern Command Design Pattern
Command Design Pattern
 
Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
 
Functional modeling
Functional modelingFunctional modeling
Functional modeling
 

Destacado

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
 

Destacado (17)

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 Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
 
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 a 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 a 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
 

Último

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
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
 

Último (20)

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 ...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
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
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
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
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
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
 
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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
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
 

Behavioral Design Patterns