Software Design Patterns Overview
Software Design Patterns Overview
>
2
>Pattern Classifications
creational design patterns are design patterns that deal with
object creation mechanisms, trying to create objects in a
manner suitable to the situation.
3
>Behavioral design patterns
Chain of responsibility
• A way of passing a request between a chain of objects
Command
• Encapsulate a command request as an object
Interpreter
• A way to include language elements in a program
Iterator
• Sequentially access the elements of a collection
Observer
• A way of notifying change to a number of classes
4
>Behavioral design patterns
State
• Alter an object's behavior when its state changes
Strategy
• Encapsulates an algorithm inside a class
Template method
• Defer the exact steps of an algorithm to a subclass
Visitor
• Defines a new operation to a class without change
Mediator
• Defines simplified communication between classes
Memento
• Capture and restore an object's internal state
5
>Sorting People
A person includes a name, an address, and an ID number. For
example:
• Name: Rutherford B. Hayes
• Address: 17 E William St, Delaware, OH 43015
• ID Number: 001-00-019
People displayed by the application should be sortable based on:
• Name
• Zip Code
• ID Number
6
>Sorting People
A person includes a name, an address, and an ID number. For
example:
• Name: Rutherford B. Hayes
• Address: 17 E William St, Delaware, OH 43015
• ID Number: 001-00-019
People displayed by the application should be sortable based on:
• Name
• Zip Code
• ID Number
7
>Option 1: Conditionals
We already know that conditionals
public void sort(int sortType) {
like this can be a code smell.What
switch(sortType) { are the drawbacks of this solution?
case LAST_NAME:
// sort people by last name
break;
case ZIP_CODE:
The class is not very
// sort people by ZIP code cohesive: it is trying to do
and then street name too many things. The sorting
break; algorithms are likely to be
case ID_NUMBER: complex, resulting in a lot of
// sort people by ID number code
}
8
>Option 2: Subclassing
10
>Strategy Pattern Structure Diagram
11
>Defining a Sorting Strategy
an interface to represent a sorting strategy. This a concrete strategy for one of the required
interface will be implemented by any class that can sorting strategies, e.g. sorting people by
sort a list of people. last name and then, if the last names
match, by first name.
12
>Interchangeable Strategies
private PeopleSorter sorter; Modify the sorting class, i.e. PeopleList, so that it
private List<Person> people; is a context on which the sorting strategy can be
changed.
public PeopleList() {
people = new ArrayList<>();
sorter = new SortByName();
}
13
>Interchangeable Strategies
14
>Interchangeable Strategies
15
>Interchangeable Strategies
public PeopleList() {
When a client calls the method to
people = new ArrayList<>();
sorter = new SortByName(); sort the list of people, the context
} will delegate the responsibility for
handling the sort to the current
public void setSorter(PeopleSorter sorter) strategy
{ this.sorter = sorter;
}
16
>People Sorter Strategy Design
17
>Strategy Pattern
Consequences
• Families of related algorithms
• An alternative to subclassing
• Elimination of conditional statements
• A choice of different implementations of the same behavior (e.g. quicksort
vs. merge sort)
• Clients must be aware of the alternatives to switch between them
• Communication overhead between Context and Strategy
• Increased number of objects in the system
18
>
19
>Clipboard Coding
The word processing application shall allow users to copy the
currently selected text to the system clipboard
• Using a Copy option in the Edit menu.
• Using a keyboard shortcut: CTRL-C
• Using the Copy option in the context menu (right click).
• Using the Copy icon in the toolbar
The application shall allow users to paste the contents of the
system clipboard into the current document
• Using a Paste option in the Edit menu.
• Using a keyboard shortcut: CTRL-V
• Using the Paste option in the context menu (right click).
How might you go about
• Using the Paste icon in the toolbar implementing this
requirement?
20
>Option 1: Embedded Code
21
>Option 1: Embedded Code
22
>Option 2: A Paste Method
23
>Option 2: A Paste Method
24
>Command Pattern
Intent:
• Encapsulate a request as an object, thereby letting you parameterize
clients with different requests, queue or log requests, and support
undoable operations.
25
>Command Pattern Structure Diagram
26
>A command interface
public interface Action { an interface to represent a command that can be
public void performAction(); executed by the user in the word processing
} application
27
>A command interface
public interface Action { an interface to represent a command that can be
public void performAction(); executed by the user in the word processing
} application
28
>A command interface
public interface Action { an interface to represent a command that can be
public void performAction(); executed by the user in the word processing
} application
30
>Generic Widgets
public class Button {
When the user interacts with the widget, it invokes
private Action action; the method on its command.
public Button(Action action) {
this.action = action; The widget does not need any information about
}
what the command actually does. It just needs to
public void buttonClicked() { invoke its command.
action.performAction();
}
This means that the same, generic widget can be
reused many times in the application with different
commands.
31
>Copy/Paste Command Design
Each widget (button, menu,
etc.) is the invoker of some
command.
32
>Copy/Paste Command Design
Each concrete command
executes one or more methods
on some receiver, e.g. the
main application.
33
>Copy/Paste Command Design
The invokers do not need to
know anything about the
specific command - only
when to invoke it.
34
>Command Pattern
Consequences
• Decouples the object that invokes an operation from the object that knows
how to perform it.
• Commands are first-class objects that can be manipulated and extended
like any other object.
• Commands can be assembled into composite commands (e.g. macros).
• New commands can be easily added because existing classes do not
need to be changed (similar to Strategy).
• Lots of little command classes.
35
>
36
>Invaders from Space
The game should be as responsive
as possible, and so each time the
user presses one of the control
keys, the game should immediately
respond in some way
• The player uses left and right arrow
keys to move their spaceship in the
corresponding direction.
• They use the spacebar to fire a
single shot at the encroaching aliens.
• If at any time the player presses the
“ESC” key, the game pauses and
they are prompted to quit. Pressing
the “y” key will quit the game. How would you implement these
Pressing the “ESC” key again will requirements?
unpause.
37
>Option 1: Polling
while(true){
try {
When using polling, execution is suspended for
Thread.sleep(INTERVAL); some interval, after which the program checks to
} catch(InterruptedException e) {} see if any significant events have occured.
switch(lastKeyPressed()) {
• Missed Events - if the polling interval is too long, it is possible that more
than one event will occur between intervals.
• Duplicate Events - it is possible that the same event may be handled more
than once, e.g. a single press of the left arrow moves the ship 2 or more
times.
39
>Observer Pattern
Intent:
• Define a one-to-many dependency between objects so that when one
object changes state, all its dependents are notified and updated
automatically.
40
>Observer Pattern Structure Diagram
41
>Observing a Subject
public interface KeyListener { an interface to be implemented by any
public void keyPressed(KeyEvent event); observers that should be notified whenever
} a key is pressed on the keyboard.
42
>Implement the Subject Interface
45
>Implement the Observer Interface
46
>Invaders from Space Observer Design
47
>Observer Pattern
Consequences
• Abstract coupling is used so that the Concrete Subject is not coupled with
Concrete Observers (and vice versa).
• Updates a broadcast to any interested observers.
• Cascading updates may occur if/when one observer modifies the subject.
48
>
49
>Superb Plumber Siblings
the player avatar starts in its small form.
• Contact with an enemy results in losing a life (character death).
• Acquiring a mushroom awards 1000 points and changes the player
avatar to its superb form.
• Acquiring a fire flower awards 1000 points and changes the player
avatar to its shooter form.
While in superb form.
• Contact with an enemy changes the player avatar to its small form.
• Acquiring a mushroom awards 1000 points.
• Acquiring a fire flower awards 1000 points and changes the player
avatar to its shooter form. How would you
While in shooter form. implement these
requirements?
• Contact with an enemy changes the player avatar to its small form.
• Pressing the fire button shoots a fireball.
• Acquiring a mushroom awards 1000 points
• Acquiring a fire flower awards 1000 points.
50
>Superb Plumber Siblings State Diagram
Number of ways that the
different states of the
player avatar may be
handled
○ Small Form
○ Superb Form
○ Shooter Form
51
>Option 1: Conditionals
Every method on the player avatar changes behavior
public void enemyContacted() { depending on the current state using a conditional to
switch(currentState) {
determine which code to execute, e.g. what to do when
case SMALL_FORM:
loseALife();
the player contacts an enemy.
break;
case SUPERB_FORM:
case SHOOTER_FORM: What are the major drawbacks of this approach?
currentState = SMALL_FORM;
shrinkAvatar();
break;
}
}
52
>Option 1: Conditionals
public void enemyContacted() { One massive class that contains all of the behavior for all
switch(currentState) { of the possible states is not very cohesive.
case SMALL_FORM:
loseALife();
break;
case SUPERB_FORM:
Adding new states (like invincibility!) requires modifying
case SHOOTER_FORM: all of the state-dependent methods (OCP).
currentState = SMALL_FORM;
shrinkAvatar();
break;
}
}
53
>Option 2: Subclassing
54
>Option 2: Subclassing
55
>State Pattern
Intent:
• Allow an object to alter its behavior when its internal state changes. The
object will appear to change its class.
56
>State Pattern Structure Diagram
57
>a State Interface
interface Form { an interface that represents the state that the player avatar
may be in
void handleEnemy();
void handleMushroom();
void handleFireFlower(); define a method for each of the behaviors that changes
void handleFireButton(); based on the current state.
58
>a Context
currentForm.handleEnemy();
}
public void mushroomAcquired() {
currentForm.handleMushroom();
}
// and so on...
}
59
>a Context
currentForm.handleEnemy();
}
public void mushroomAcquired() {
currentForm.handleMushroom();
}
// and so on...
}
60
>a Context
currentForm.handleEnemy();
}
public void mushroomAcquired() {
currentForm.handleMushroom();
}
// and so on...
}
61
>Concrete States
SmallForm(PlayerAvatar avatar) {
this.avatar = avatar;
}
62
>Concrete States
SmallForm(PlayerAvatar avatar) {
this.avatar = avatar; the concrete state has a reference to the
} context which is used to change the state
and/or call methods on the context.
public void handleEnemy() {
avatar.loseALife();
}
63
>Concrete States
SmallForm(PlayerAvatar avatar) {
this.avatar = avatar; the concrete state has a reference to the
} context which is used to change the state
and/or call methods on the context.
public void handleEnemy() {
avatar.loseALife();
}
64
>Superb Plumber Siblings State Design
65
>State Pattern
Consequences
• State-specific behavior is partitioned into separate objects.
• State-transitions become explicit and the context is protected from
inconsistent internal states.
• Managing the transition between states can be messy or tricky.
• The context may be a “state holder.”
• Class Explosion (lots of states).
66
>
67
>
68