SWE 316: Software Design and Architecture
Obj
ectiv
esLecture # 15
Behavioral Design Patterns
SWE 316: Software Design and Architecture
To learn the behavioral design patterns and when to use them.
Ch 9Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
SWE 316: Software Design and Architecture
Patterns Behavioral Design Patterns capture behavior
among objects
Interpreter Iterator Mediator Observer State Chain of Responsibility Command Template
Interpreter Iterator Mediator Observer State Chain of Responsibility 2/22
SWE 316: Software Design and Architecture
Interpreter Design Pattern Design Purpose
Interpret expressions written in a formal grammar.
Design Pattern Summary Represent the grammar using a Recursive
design pattern form: Pass interpretation to aggregated objects.
Interpreter Iterator Mediator Observer State Chain of Responsibility 3/22
SWE 316: Software Design and Architecture
AbstractExpressioninterpret()Client
TerminalExpressioninterpret()
NonTerminalExpressioninterpret()
1..n
Interpreter Design Pattern
-- a form for parsing and a means of processing expressions.
KEY CONCEPTDesign Goal At Work: Flexibility , Correctness, Reuse
Interpreter Iterator Mediator Observer State Chain of Responsibility 4/22
SWE 316: Software Design and Architecture
Purpose of Iteratorgiven a collection of objects
e.g., the videos in a video store a directory
having specified ways to progress through them e.g., “list in alphabetical order” “list all videos currently on loan”
... encapsulate each of these ways
Aggregate object
iterator2
iterator7
Interpreter Iterator Mediator Observer State Chain of Responsibility 5/22
SWE 316: Software Design and Architecture
Iterator Design PatternDesign Purpose
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Design Pattern Summary Encapsulate the iteration in a class
pointing (in effect) to an element of the aggregate.
Interpreter Iterator Mediator Observer State Chain of Responsibility 6/22
SWE 316: Software Design and Architecture
/*
To perform desiredOperation() on elements of the aggregate according to the iteration (order) i:
*/
for( i.setToFirst(); !i.isDone(); i.increment() )
desiredOperation( i.getCurrentElement() );
Using Iterator FunctionsInterpreter Iterator Mediator Observer State Chain of
Responsibility 7/22
SWE 316: Software Design and Architecture
// Iterator "points" to first element:
void setToFirst();
// true if iterator "points" past the last element:
boolean isDone();
// Causes the iterator to point to its next element:
void increment();
// Return the element pointed to by the iterator:
C getCurrentElement();
Functions for IteratorInterpreter Iterator Mediator Observer State Chain of
Responsibility 8/22
SWE 316: Software Design and Architecture
Iterator in Arrays, Vector, and in General
Iterator Operations
Set to beginning Increment Get current
elementCheck not done yet
The Iterator
Index (integer) i on
array myArrayi = 0 ++i myArray[ i ] i < myArray
.length
Index (integer) j on
VectormyVector
j = 0 ++j myVector.get( j )
j < myVector.size()
Iterator (object)myIterator
myIterator.setToFirst()
myIterator.increment()
myIterator.getCurrent Element()
! myIterator.isDone()
Interpreter Iterator Mediator Observer State Chain of Responsibility 9/22
SWE 316: Software Design and Architecture
iterator:Iterator
element:Element
Aggregate of Elementobjects
Key: Intended sequence of Element objects
After first() executes, iterator references this object.
After increment() executes, iterator references this object.
Before increment() executes, iterator references this object.
Imagining IteratorInterpreter Iterator Mediator Observer State Chain of
Responsibility 10/22
SWE 316: Software Design and Architecture
MediatorDesign Purpose
Avoid references between dependent objects.
Design Pattern Summary Capture mutual behavior in a
separate class.
Interpreter Iterator Mediator Observer State Chain of Responsibility 11/22
SWE 316: Software Design and Architecture
Solicitation of Customer Information 1 of 2
Name and Location
Basic information
Interpreter Iterator Mediator Observer State Chain of Responsibility 12/22
SWE 316: Software Design and Architecture
Solicitation of Customer Information 2 of 2
Account Information
Additional information
Customer ID
Total business
Amount due
Interpreter Iterator Mediator Observer State Chain of Responsibility 13/22
SWE 316: Software Design and Architecture
Mediator Colleague
ConcreteMediator
ConcreteColleague1 ConcreteColleague2
The Mediator Class ModelInterpreter Iterator Mediator Observer State Chain of
Responsibility 14/22
SWE 316: Software Design and Architecture
Observer Design Pattern Design Purpose
Arrange for a set of objects to be affected by a single object.
to keep a set of objects up to date with the state of a designated object
Design Pattern Summary The single object aggregates the set,
calling a method with a fixed name on each member.
Interpreter Iterator Mediator Observer State Chain of Responsibility 15/22
SWE 316: Software Design and Architecture
Sourcenotify()
Observerupdate()
for all Observer’s o:o.update();
Client of thissystem1
2
1..n
Server part Client part
Observer Design PatternInterpreter Iterator Mediator Observer State Chain of
Responsibility 16/22
SWE 316: Software Design and Architecture
Sourcenotify()
Observerupdate()
ConcreteSourcestate
ConcreteObserverobserverState
update()
for all Observer’s o: o.update();
Client
2
3
1..n
Server part Client part
1
Observer Design PatternInterpreter Iterator Mediator Observer State Chain of
Responsibility 17/22
SWE 316: Software Design and Architecture
State Design PatternDesign Purpose
Cause an object to behave in a manner determined by its state.
Design Pattern Summary Aggregate a State object and
delegate behavior to it.
Interpreter Iterator Mediator Observer State Chain of Responsibility 18/22
SWE 316: Software Design and Architecture
TargetStateBhandleRequest()
TargetdoRequest()
doRequest() behaves according to state of Target
targetState TargetStatehandleRequest()
Client
TargetStateAhandleRequest()
1
{ targetState.handleRequest(); }
. . . . . .
State Design Pattern StructureInterpreter Iterator Mediator Observer State Chain of
Responsibility 19/22
SWE 316: Software Design and Architecture
Chain of Responsibility Design Pattern Design Purpose
Allow a set of objects to service a request. Present clients with a simple interface.
to distribute functional responsibility among a collection of objects.
Design Pattern Summary Link the objects in a chain via
aggregation, allowing each to perform some of the responsibility, passing the request along.
Interpreter Iterator Mediator Observer State Chain of Responsibility 20/22
SWE 316: Software Design and Architecture
Other Patterns Command Template
Will not be covered
Interpreter Iterator Mediator Observer State Chain of Responsibility 21/22
SWE 316: Software Design and Architecture
Summary of Behavioral PatternsBehavioral Design Patterns capture behavior among
objects Interpreter handles expressions in grammers Iterator visits members of a collection Mediator captures behavior among peer objects Observer updates objects affected by a single
object State allows method behavior to depend on
current status Chain of Responsibility allows a set of objects to
provide functionality collectively Command captures function flexibly (e.g. undo-
able) Template captures basic algorithms, allowing
variability
Interpreter Iterator Mediator Observer State Chain of Responsibility 22/22
Top Related