CSE 213: Software Engineering
Behavioral Design Patterns
Nafis Tahmid
Lecturer, CSE, BUET
Nafis Tahmid, Lecturer, CSE
What are behavioral design patterns?
❏ Behavioral patterns deal with how objects behave
and interact; defining communication,
responsibility flow and how tasks are carried out
between objects etc.
❏ Concerned with algorithms and assignment of
responsibilities between objects.
Nafis Tahmid, Lecturer, CSE 2
What are behavioral design patterns?
❏ Behavioral patterns deal with how objects behave
and interact; defining communication,
responsibility flow and how tasks are carried out
between objects etc.
❏ Concerned with algorithms and assignment of
responsibilities between objects.
Nafis Tahmid, Lecturer, CSE 3
Simple Duck Simulator
Duck species can -
- Swim
- Quack
They quack & swim the same way!
But look different!!
Nafis Tahmid, Lecturer, CSE 4
Simple Duck Simulator
Duck species can -
- Swim
- Quack
They quack & swim the same way!
But look different!!
Nafis Tahmid, Lecturer, CSE 5
Simple Duck Simulator
Duck species can -
- Swim
- Quack
They quack & swim the same way!
But look different!!
Now we need to make the ducks fly!
Nafis Tahmid, Lecturer, CSE 6
Simple Duck Simulator
Duck species can -
- Swim
- Quack
They quack & swim the same way!
But look different!!
Now we need to make the ducks fly!
Nafis Tahmid, Lecturer, CSE 7
Simple Duck Simulator
At the project show, RUBBER DUCK starts flying!
Nafis Tahmid, Lecturer, CSE 8
Solution?
Override the ones that don’t fly?
Nafis Tahmid, Lecturer, CSE 9
Solution?
Override the ones that don’t fly?
What if we add a wooden decoy duck which
doesn’t squeak either?
Nafis Tahmid, Lecturer, CSE 10
Solution?
What if we add a wooden decoy duck which
doesn’t squeak either?
Nafis Tahmid, Lecturer, CSE 11
Solution?
What if we add a wooden decoy duck which
doesn’t squeak either?
Bad design
Nafis Tahmid, Lecturer, CSE 12
Another Approach?
Now we know that new features will be added/updated every six months
Obviously, adding new ducks and overriding fly(), quack() for each is not an option
What to do…
Nafis Tahmid, Lecturer, CSE 13
Another Approach - Using Interfaces?
Flyable and Quackable are interfaces
Those who fly and/or quack, implement those.
Nafis Tahmid, Lecturer, CSE 14
Another Approach - Using Interfaces?
Flyable and Quackable are interfaces
Those who fly and/or quack, implement those.
Nafis Tahmid, Lecturer, CSE 15
Another Approach - Using Interfaces?
Flyable and Quackable are interfaces
Those who fly and/or quack, implement those.
Is it good enough?
Nafis Tahmid, Lecturer, CSE 16
Another Approach - Using Interfaces?
Flyable and Quackable are interfaces
Those who fly and/or quack, implement those.
What if we have to change fly behavior slightly?
Say wingbeat rate, angle etc.
Nafis Tahmid, Lecturer, CSE 17
Another Approach - Using Interfaces?
Flyable and Quackable are interfaces
Those who fly and/or quack, implement those.
What if we have to change fly behavior slightly? CHANGE ALL IMPLEMENTATIONS!!!
Say wingbeat rate, angle etc.
Nafis Tahmid, Lecturer, CSE 18
Another Principle
Nafis Tahmid, Lecturer, CSE 19
Let’s follow the principle
Nafis Tahmid, Lecturer, CSE 20
Let’s follow the principle
Nafis Tahmid, Lecturer, CSE 21
Let’s follow the principle
Nafis Tahmid, Lecturer, CSE 22
Let’s follow the principle
Nafis Tahmid, Lecturer, CSE 23
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 24
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 25
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 26
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 27
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 28
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 29
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 30
Let’s see what we have done
Nafis Tahmid, Lecturer, CSE 31
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 32
Let’s make behavior “DYNAMIC”
new duck
can’t fly
Nafis Tahmid, Lecturer, CSE 33
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 34
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 35
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 36
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 37
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 38
Let’s make behavior “DYNAMIC”
Nafis Tahmid, Lecturer, CSE 39
Birds Eye View
Nafis Tahmid, Lecturer, CSE 40
Birds Eye View
Nafis Tahmid, Lecturer, CSE 41
Yet another example
Nafis Tahmid, Lecturer, CSE 42
Yet another example
In map app, you can change different strategies in real-time between two different points!
Nafis Tahmid, Lecturer, CSE 43
Yet another example
In map app, you can change different strategies in real-time between two different points!
Nafis Tahmid, Lecturer, CSE 44
Strategy Pattern Defined
Define a family of algorithms, encapsulate each
one, and make them interchangeable. Strategy
lets the algorithm vary independently from the
clients that use it.
Nafis Tahmid, Lecturer, CSE 45
Strategy Pattern Defined
Nafis Tahmid, Lecturer, CSE 46
Nafis Tahmid, Lecturer, CSE 47
There are no DUMB Questions
Nafis Tahmid, Lecturer, CSE 48
It’s time for some caffeine
Nafis Tahmid, Lecturer, CSE 49
Let’s see some details…
Nafis Tahmid, Lecturer, CSE 50
Let’s see some details…
Nafis Tahmid, Lecturer, CSE 51
Let’s see some details…
exactly same!
Nafis Tahmid, Lecturer, CSE 52
Let’s see some details…
Some steps vary!
Nafis Tahmid, Lecturer, CSE 53
Doing simple abstractions? [keeping what is unchanged in abstract class]
Nafis Tahmid, Lecturer, CSE 54
Doing simple abstractions? [keeping what is unchanged in abstract class]
Nafis Tahmid, Lecturer, CSE 55
Doing simple abstractions…; is it a good design?
Nafis Tahmid, Lecturer, CSE 56
Don’t they have the same “skeleton”?
Nafis Tahmid, Lecturer, CSE 57
Don’t they have the same “skeleton”?
Nafis Tahmid, Lecturer, CSE 58
Don’t they have the same “skeleton”?
Nafis Tahmid, Lecturer, CSE 59
Don’t they have the same “skeleton”?
Make prepareRecipe() abstract?
Nafis Tahmid, Lecturer, CSE 60
Keep the common steps in abstract class…
Nafis Tahmid, Lecturer, CSE 61
Let subclasses worry about the steps that differ…
Nafis Tahmid, Lecturer, CSE 62
What have we done?
Nafis Tahmid, Lecturer, CSE 63
Template Method
Nafis Tahmid, Lecturer, CSE 64
Template Method
Nafis Tahmid, Lecturer, CSE 65
Let’s make some tea!
Nafis Tahmid, Lecturer, CSE 66
Let’s make some tea!
Common
Different
Nafis Tahmid, Lecturer, CSE 67
Let’s make some tea!
Common
Different
Nafis Tahmid, Lecturer, CSE 68
Let’s make some tea!
Common
Different
Nafis Tahmid, Lecturer, CSE 69
Let’s make some tea!
Common
Different
Nafis Tahmid, Lecturer, CSE 70
Let’s make some tea!
Common
Different
Nafis Tahmid, Lecturer, CSE 71
Adding conditional executions with if-else
Nafis Tahmid, Lecturer, CSE 72
Template Method Pattern Defined
Template Method pattern defines the skeleton of
an algorithm in the superclass but lets subclasses
override specific steps of the algorithm without
changing its structure.
Nafis Tahmid, Lecturer, CSE 73
The generalized structure
It is final method
Nafis Tahmid, Lecturer, CSE 74
Yet another example: Data Mining
Nafis Tahmid, Lecturer, CSE 75
Yet another example: Data Mining
only these vary
Nafis Tahmid, Lecturer, CSE 76
Solution…
Nafis Tahmid, Lecturer, CSE 77
Difference between Strategy and Template
➢ Template Method based on inheritance
- Alter parts of an algorithm by extending those parts in subclasses.
➢ Strategy is based on composition
- Alter parts of the object’s behavior by supplying it with different strategies
that correspond to that behavior.
➢ Template Method works at the class level, so it’s static.
➢ Strategy works on the object level, letting you switch behaviors at runtime.
Nafis Tahmid, Lecturer, CSE 78
Nafis Tahmid, Lecturer, CSE 79
Nafis Tahmid, Lecturer, CSE 80
Nafis Tahmid, Lecturer, CSE 81
Now let’s talk about weather monitoring system
Nafis Tahmid, Lecturer, CSE 82
Now let’s talk about weather monitoring system
Physical device that acquires actual weather data
Nafis Tahmid, Lecturer, CSE 83
Now let’s talk about weather monitoring system
Tracks data coming from weather station & updates displays
Nafis Tahmid, Lecturer, CSE 84
Now let’s talk about weather monitoring system
Show current weather conditions
Nafis Tahmid, Lecturer, CSE 85
Let’s see what we have got from Weather-o-Rama
Nafis Tahmid, Lecturer, CSE 86
Let’s see what we have got from Weather-o-Rama
Change triggers it
Nafis Tahmid, Lecturer, CSE 87
What we need to do?
Implement three displays
❏ One: current conditions
❏ Two: statistics
❏ Three: forecast
Nafis Tahmid, Lecturer, CSE 88
What we need to do?
Implement three displays
❏ One: current conditions
❏ Two: statistics
❏ Three: forecast
Remember expandability; display can be added/removed
Nafis Tahmid, Lecturer, CSE 89
Misguided implementation…
Nafis Tahmid, Lecturer, CSE 90
Misguided implementation…
get results
Nafis Tahmid, Lecturer, CSE 91
Misguided implementation…
update displays
Nafis Tahmid, Lecturer, CSE 92
Misguided implementation…
Nafis Tahmid, Lecturer, CSE 93
Misguided implementation…
Nafis Tahmid, Lecturer, CSE 94
How magazine subscriptions work?
Nafis Tahmid, Lecturer, CSE 95
How magazine subscriptions work?
Nafis Tahmid, Lecturer, CSE 96
How magazine subscriptions work?
Nafis Tahmid, Lecturer, CSE 97
How magazine subscriptions work?
Nafis Tahmid, Lecturer, CSE 98
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 99
Let’s take a closer look
observers interested in it
Nafis Tahmid, Lecturer, CSE 100
Let’s take a closer look
observers interested in it
Nafis Tahmid, Lecturer, CSE 101
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 102
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 103
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 104
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 105
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 106
Let’s take a closer look
Nafis Tahmid, Lecturer, CSE 107
subject
observers
Nafis Tahmid, Lecturer, CSE 108
So in weather station…
Nafis Tahmid, Lecturer, CSE 109
Our new design
Nafis Tahmid, Lecturer, CSE 110
Our new design
list of observers
Interface referencing WeatherData object
Nafis Tahmid, Lecturer, CSE 111
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 112
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 113
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 114
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 115
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 116
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 117
Let’s see some code…
passed in constructor
Nafis Tahmid, Lecturer, CSE 118
Let’s see some code…
How it registers itself
Nafis Tahmid, Lecturer, CSE 119
Let’s see some code…
display() called
Nafis Tahmid, Lecturer, CSE 120
Observer Pattern Defined
Define a one-to-many dependency between objects
so that when one object changes state, all its
dependents are notified and updated automatically.
Nafis Tahmid, Lecturer, CSE 121
Observer Pattern Defined
Define a one-to-many dependency between objects
so that when one object changes state, all its
dependents are notified and updated automatically.
Nafis Tahmid, Lecturer, CSE 122
Observer Pattern Defined
Publisher == Subject
Subscriber == Observer
Nafis Tahmid, Lecturer, CSE 123
Observer Pattern Defined
Publisher == Subject
Subscriber == Observer
Nafis Tahmid, Lecturer, CSE 124
Observer Pattern Defined
Publisher == Subject
Subscriber == Observer
Nafis Tahmid, Lecturer, CSE 125
Observer Pattern Defined
Publisher == Subject
Subscriber == Observer
Nafis Tahmid, Lecturer, CSE 126
Observer Pattern Defined
Publisher == Subject
Subscriber == Observer
Nafis Tahmid, Lecturer, CSE 127
Nafis Tahmid, Lecturer, CSE 128
Air traffic control
During the landing of aircrafts -
❏ Aircraft pilots don’t talk to each other directly
❏ All communication goes through the control tower.
Nafis Tahmid, Lecturer, CSE 129
Air traffic control
During the landing of aircrafts -
❏ Aircraft pilots don’t talk to each other directly
❏ All communication goes through the control tower.
Without control tower -
❖ Pilots need to be aware of every other aircrafts
❖ Airplane crash statistics would probably skyrocket
Nafis Tahmid, Lecturer, CSE 130
Now let’s discuss UI design for a customer profile management
A dialog for creating and editing
customer profiles.
Generated using LaTeX. Thanks to Claude Nafis Tahmid, Lecturer, CSE 131
Now let’s discuss UI design for a customer profile management
A dialog for creating and editing
customer profiles.
- Various form controls such
as text fields, checkboxes,
buttons etc.
- Some interact with others
Generated using LaTeX. Thanks to Claude Nafis Tahmid, Lecturer, CSE 132
Now let’s discuss UI design for a customer profile management
A dialog for creating and editing
customer profiles.
- Various form controls such
as text fields, checkboxes,
buttons etc.
- Some interact with others
- Ex: selecting “Business”
enables company name
Generated using LaTeX. Thanks to Claude Nafis Tahmid, Lecturer, CSE 133
Now let’s discuss UI design for a customer profile management
A dialog for creating and editing
customer profiles.
- Various form controls such
as text fields, checkboxes,
buttons etc.
- Some interact with others
- Ex: “Apply” validates
everything
Generated using LaTeX. Thanks to Claude Nafis Tahmid, Lecturer, CSE 134
Now let’s discuss UI design for a customer profile management
A dialog for creating and editing
customer profiles.
- Various form controls such
as text fields, checkboxes,
buttons etc.
- Some interact with others
Generated using LaTeX. Thanks to Claude Nafis Tahmid, Lecturer, CSE 135
What we may think of doing initially…
Inside the elements (i.e in their class) implement interaction the logics directly.
Nafis Tahmid, Lecturer, CSE 136
What we may think of doing initially…
Inside the elements (i.e in their class) implement interaction the logics directly.
Example:
In the checkbox -
● Logic 1 for interaction with the button
● Logic 2 for interaction with the textfield
Nafis Tahmid, Lecturer, CSE 137
What we may think of doing initially…
Inside the elements (i.e in their class) implement interaction the logics directly.
Example:
In the checkbox -
● Logic 1 for interaction with the button
● Logic 2 for interaction with the textfield
Problem -
Can we use the checkbox class in another form?
Nafis Tahmid, Lecturer, CSE 138
What we may think of doing initially…
Inside the elements (i.e in their class) implement interaction the logics directly.
Example:
In the checkbox -
● Logic 1 for interaction with the button
● Logic 2 for interaction with the textfield
Problem -
Can we use the checkbox class in another form?
- NO. As it uses 1 button, 1 textfield, 1 dialogue
Nafis Tahmid, Lecturer, CSE 139
What we may think of doing initially…
Inside the elements (i.e in their class) implement interaction the logics directly.
Example:
In the checkbox -
● Logic 1 for interaction with the button
● Logic 2 for interaction with the textfield
Problem -
Can we use the checkbox class in another form?
- NO. As it uses 1 button, 1 textfield, 1 dialogue
(coupling)
Nafis Tahmid, Lecturer, CSE 140
Solution?
❏ Cease all direct communication between the components
Nafis Tahmid, Lecturer, CSE 141
Solution?
❏ Cease all direct communication between the components
❏ Must collaborate indirectly, through a mediator, who redirects call to appropriate
components
Nafis Tahmid, Lecturer, CSE 142
Solution?
❏ Cease all direct communication between the components
❏ Must collaborate indirectly, through a mediator, who redirects call to appropriate
components
Dialog is mediator
Nafis Tahmid, Lecturer, CSE 143
Solution?
❏ Cease all direct communication between the components
❏ Must collaborate indirectly, through a mediator, who redirects call to appropriate
components
Dialog is mediator
Components depend only on a single mediator class instead of being coupled to dozens of their colleagues.
Nafis Tahmid, Lecturer, CSE 144
So, in UI design…
❏ Dialogue class is the mediator.
Nafis Tahmid, Lecturer, CSE 145
So, in UI design…
❏ Dialogue class is the mediator.
❏ Dialogue should know all of its sub-elements
(components)
Nafis Tahmid, Lecturer, CSE 146
So, in UI design…
❏ Dialogue class is the mediator.
❏ Dialogue should know all of its sub-elements
(components)
- No new dependency
Nafis Tahmid, Lecturer, CSE 147
So, in UI design…
❏ Dialogue class is the mediator.
❏ Dialogue should know all of its sub-elements
(components)
- No new dependency
❏ Previously, “apply” button had to know all
the other elements.
Nafis Tahmid, Lecturer, CSE 148
So, in UI design…
❏ Dialogue class is the mediator.
❏ Dialogue should know all of its sub-elements
(components)
- No new dependency
❏ Now, “apply” just notifies mediator (i.e the
dialogue box)
Nafis Tahmid, Lecturer, CSE 149
So, in UI design…
❏ Dialogue class is the mediator.
❏ Dialogue should know all of its sub-elements
(components)
- No new dependency
❏ Now, “apply” just notifies mediator (i.e the
dialogue box). “Apply” needs to know who it
is notifying…
Nafis Tahmid, Lecturer, CSE 150
Birds Eye View
Nafis Tahmid, Lecturer, CSE 151
Birds Eye View
Dialog needs to know who are there
Nafis Tahmid, Lecturer, CSE 152
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 153
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 154
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 155
Let’s see some code…
Nafis Tahmid, Lecturer, CSE 156
The Mediator Pattern Defined
Mediator pattern restricts direct communications between
objects and forces them to collaborate only via a mediator.
Lets you reduce chaotic dependencies between objects.
Nafis Tahmid, Lecturer, CSE 157
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 158
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 159
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 160
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 161
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 162
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 163
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 164
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 165
The Mediator Pattern Defined
Nafis Tahmid, Lecturer, CSE 166
Difference between Mediator and Observer
Primary goal:
- Mediator eliminates mutual dependencies. Instead, objects depend on single
mediator object.
- Observer establishes dynamic (can subscribe, unsubscribe) one-way one-to-many
connection between objects.
Nafis Tahmid, Lecturer, CSE 167
Let’s design a simple text-editor
Generated using LaTeX. Thanks to Claude
Nafis Tahmid, Lecturer, CSE 168
Let’s design a simple text-editor
Create a toolbar with a bunch of buttons for various
operations of the editor
Generated using LaTeX. Thanks to Claude
Nafis Tahmid, Lecturer, CSE 169
Let’s design a simple text-editor
Create a toolbar with a bunch of buttons for various
operations of the editor
How to design buttons?
Generated using LaTeX. Thanks to Claude
Nafis Tahmid, Lecturer, CSE 170
Let’s design a simple text-editor
Create a toolbar with a bunch of buttons for various
operations of the editor
A neat button class used for toolbar buttons? [look
same]
Generated using LaTeX. Thanks to Claude
Nafis Tahmid, Lecturer, CSE 171
Let’s design a simple text-editor
Create a toolbar with a bunch of buttons for various
operations of the editor
But they do different things…
Generated using LaTeX. Thanks to Claude
Nafis Tahmid, Lecturer, CSE 172
Let’s design a simple text-editor
Create a toolbar with a bunch of buttons for various
operations of the editor
But they do different things…
Subclasses to do different things when clicked?
Generated using LaTeX. Thanks to Claude
Nafis Tahmid, Lecturer, CSE 173
Subclasses Issues…
Enormous number of subclasses
- Modifying Button class risks breaking code!
Nafis Tahmid, Lecturer, CSE 174
Subclasses Issues…
Enormous number of subclasses
- Modifying Button class risks breaking code!
Ugliest part
- Save can be done by Ctrl+S (shortcut) too.
- From menu (File -> Save) also.
Nafis Tahmid, Lecturer, CSE 175
Subclasses Issues…
Enormous number of subclasses
- Modifying Button class risks breaking code!
Ugliest part
- Save can be done by Ctrl+S (shortcut) too.
- From menu (File -> Save) also.
- Code duplication!! (write same operations multiple times)
Nafis Tahmid, Lecturer, CSE 176
Subclasses Issues…
Enormous number of subclasses
- Modifying Button class risks breaking code!
Ugliest part
- Save can be done by Ctrl+S (shortcut) too.
- From menu (File -> Save) also.
- Code duplication!! (write same operations multiple times)
Solution?
- Invoke button click handler from shortcut or save menu?
Nafis Tahmid, Lecturer, CSE 177
Subclasses Issues…
Enormous number of subclasses
- Modifying Button class risks breaking code!
Ugliest part
- Save can be done by Ctrl+S (shortcut) too.
- From menu (File -> Save) also.
- Code duplication!! (write same operations multiple times)
Solution?
- Invoke button click handler from shortcut or save menu?
- Menu (& shortcut) dependent on buttons. Too ugly
Nafis Tahmid, Lecturer, CSE 178
Separation of concerns…
A layer for the GUI and another layer for the business logic.
Nafis Tahmid, Lecturer, CSE 179
Separation of concerns…
A layer for the GUI and another layer for the business logic.
- GUI does the job of rendering pictures, capturing input
Nafis Tahmid, Lecturer, CSE 180
Separation of concerns…
A layer for the GUI and another layer for the business logic.
- GUI does the job of rendering pictures, capturing input
- Calculating, data loading stuffs done by underlying business logic
Nafis Tahmid, Lecturer, CSE 181
Separation of concerns…
A layer for the GUI and another layer for the business logic.
- GUI does the job of rendering pictures, capturing input
- Calculating, data loading stuffs done by underlying business logic
- GUI delegates by requesting with arguments
Nafis Tahmid, Lecturer, CSE 182
Separation of concerns…
A layer for the GUI and another layer for the business logic.
- GUI does the job of rendering pictures, capturing input
- Calculating, data loading stuffs done by underlying business logic
- GUI delegates by requesting with arguments
Nafis Tahmid, Lecturer, CSE 183
Separation of concerns…
● GUI objects shouldn’t send these requests directly either
Nafis Tahmid, Lecturer, CSE 184
Separation of concerns…
● GUI objects shouldn’t send these requests directly either
● Rather the save operation should be in a single “Command” class
Nafis Tahmid, Lecturer, CSE 185
Again that “Code to interface…”
● Commands implement the same interface
● A single execute() method
Nafis Tahmid, Lecturer, CSE 186
Again that “Code to interface…”
● Commands implement the same interface
● A single execute() method
● Command parameters? Should be pre-configured, they know what to do!
Nafis Tahmid, Lecturer, CSE 187
Again that “Code to interface…”
● Commands implement the same interface
● A single execute() method
● Command parameters? Should be pre-configured, they know what to do!
Nafis Tahmid, Lecturer, CSE 188
Birds eye view
Nafis Tahmid, Lecturer, CSE 189
Birds eye view
Commands know editor
Nafis Tahmid, Lecturer, CSE 190
Birds eye view
Undo operation:
Commands that change editor state
- Push a backup copy in stack
- Undo pops the state and
restore it
Nafis Tahmid, Lecturer, CSE 191
Command Pattern Defined
Command pattern that turns a request into a stand-alone
object that contains all information about the request.
This transformation lets you parameterize methods with
different requests, delay or queue a request’s execution,
and support undoable operations.
Nafis Tahmid, Lecturer, CSE 192
Command Pattern Defined
Command pattern that turns a request into a stand-alone
object that contains all information about the request.
This transformation lets you parameterize methods with
different requests, delay or queue a request’s execution,
and support undoable operations.
passing a request as an argument
Nafis Tahmid, Lecturer, CSE 193
Command Pattern Defined
Nafis Tahmid, Lecturer, CSE 194
Command Pattern Defined
Nafis Tahmid, Lecturer, CSE 195
Command Pattern Defined
Nafis Tahmid, Lecturer, CSE 196
Command Pattern Defined
Nafis Tahmid, Lecturer, CSE 197
Command Pattern Defined
operating system?
Nafis Tahmid, Lecturer, CSE 198
Command Pattern Defined
Nafis Tahmid, Lecturer, CSE 199
Command Pattern Defined
Nafis Tahmid, Lecturer, CSE 200
Queuing request in Command Pattern
Nafis Tahmid, Lecturer, CSE 201
Difference Between Command and Mediator
● Command establishes unidirectional connections between senders and receivers.
● Mediator eliminates direct connections between senders and receivers, forcing them to
communicate indirectly via a mediator object.
Nafis Tahmid, Lecturer, CSE 202
The Gumball Machine
Nafis Tahmid, Lecturer, CSE 203
The Gumball Machine Workflow
>
Nafis Tahmid, Lecturer, CSE 204
Let’s try to write code…
Nafis Tahmid, Lecturer, CSE 205
Let’s try to write code…
Nafis Tahmid, Lecturer, CSE 206
Let’s try to write code…
Current State 207
Nafis Tahmid, Lecturer, CSE
Let’s try to write code…
Nafis Tahmid, Lecturer, CSE 208
The Gumball Machine, how states are managed?
Nafis Tahmid, Lecturer, CSE 209
The Gumball Machine, how states are managed?
Start at no quarter state
Nafis Tahmid, Lecturer, CSE 210
The Gumball Machine, action codes…
Nafis Tahmid, Lecturer, CSE 211
The Gumball Machine, action codes…
Problems?
Nafis Tahmid, Lecturer, CSE 212
The Gumball Machine, action codes…
Problems?
The nightmare of change!!
Nafis Tahmid, Lecturer, CSE 213
The Gumball Machine, action codes…
Problems?
The nightmare of change!!
To scale the sales, new policy:
10% of the time, customer
wins a free gumball! (Buy one,
get one!)
Nafis Tahmid, Lecturer, CSE 214
Updated State Diagram
Nafis Tahmid, Lecturer, CSE 215
The Gumball Machine, action codes…
Problems?
The nightmare of change!!
To scale the sales, new policy:
10% of the time, customer
wins a free gumball! (Buy one,
get one!)
Rewrite all action functions?
insertQuarter()
ejectQuarter()
turnCrank()
dispense()
Nafis Tahmid, Lecturer, CSE 216
The new design
Nafis Tahmid, Lecturer, CSE 217
The new design
Nafis Tahmid, Lecturer, CSE 218
The new design
Nafis Tahmid, Lecturer, CSE 219
State classes…
Nafis Tahmid, Lecturer, CSE 220
State classes…
Nafis Tahmid, Lecturer, CSE 221
State classes…
Machine ref. passed
Nafis Tahmid, Lecturer, CSE 222
State classes…
Knows who’s next
Nafis Tahmid, Lecturer, CSE 223
State classes…
Invalid Ops.
Nafis Tahmid, Lecturer, CSE 224
The change in the machine
Nafis Tahmid, Lecturer, CSE 225
The change in the machine
Nafis Tahmid, Lecturer, CSE 226
The change in the machine
Nafis Tahmid, Lecturer, CSE 227
The change in the machine action functions…
Nafis Tahmid, Lecturer, CSE 228
The change in the machine action functions…
No change when new
state is added
Nafis Tahmid, Lecturer, CSE 229
What we have done so far…
state.
Nafis Tahmid, Lecturer, CSE 230
What we have done so far…
state.
state.
Nafis Tahmid, Lecturer, CSE 231
What we have done so far…
state.
state.
Nafis Tahmid, Lecturer, CSE 232
What we have done so far…
state.
Nafis Tahmid, Lecturer, CSE 233
What we have done so far…
state.
Nafis Tahmid, Lecturer, CSE 234
What we have done so far…
Nafis Tahmid, Lecturer, CSE 235
Winner State…
Nafis Tahmid, Lecturer, CSE 236
Change in GumballMachine
Nafis Tahmid, Lecturer, CSE 237
Winner State
Nafis Tahmid, Lecturer, CSE 238
Winner State…
Nafis Tahmid, Lecturer, CSE 239
Winner State
Nafis Tahmid, Lecturer, CSE 240
The State Pattern Defined
Allow an object to alter its behavior when
its internal state changes. The object will
appear to change its class.
Nafis Tahmid, Lecturer, CSE 241
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 242
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 243
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 244
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 245
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 246
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 247
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 248
The State Pattern Defined
Nafis Tahmid, Lecturer, CSE 249
There are no DUMB Questions
Nafis Tahmid, Lecturer, CSE 250
There are no DUMB Questions
Nafis Tahmid, Lecturer, CSE 251
Yet another example
The publish method of the document works a
little bit differently in each state
Nafis Tahmid, Lecturer, CSE 252
Yet another example
Nafis Tahmid, Lecturer, CSE 253
Difference between State and Strategy
● They are twins separated at birth
● Both based on composition
● Change the behavior of the context by delegating some work to helper objects.
Nafis Tahmid, Lecturer, CSE 254
Difference between State and Strategy
● They are twins separated at birth
● Both based on composition
● Change the behavior of the context by delegating some work to helper objects.
● Strategy makes these objects completely independent and unaware of each other.
Nafis Tahmid, Lecturer, CSE 255
Difference between State and Strategy
● They are twins separated at birth
● Both based on composition
● Change the behavior of the context by delegating some work to helper objects.
● Strategy makes these objects completely independent and unaware of each other.
● State doesn’t restrict dependencies between concrete states, letting them alter the state of
the context at will.
Nafis Tahmid, Lecturer, CSE 256
Nafis Tahmid, Lecturer, CSE 257
Nafis Tahmid, Lecturer, CSE 258
Nafis Tahmid, Lecturer, CSE 259
Nafis Tahmid, Lecturer, CSE 260
Acknowledgement
❏ Dive Into Design Patterns - Alexander Shvets
❏ Head First Design Patterns, 2nd Edition - Eric Freeman, Elisabeth Robson
Nafis Tahmid, Lecturer, CSE 261