Chapter 15
Chapter 15
companyName
companyAddress
companyRegistrationNumber
getCompanyDetails( )
- companyInstance Class-scope
- companyName (or static) attribute
- companyAddress
- companyRegistrationNumber Class-scope
+ getCompanyInstance( ) (or static) operation
+ getCompanyDetails( )
- Company( ) Private constructor
Singleton
MediaClip
play( )
VideoClip SoundClip
play( ) play( )
getChild( )
1
play() is
polymorphically
redefined
* *
VideoClip SoundClip
play( ) play( )
play( ) * {Ordered}
addClip( )
play()is removeClip( )
polymorphically
redefined getChild( )
1
VideoClip SoundClip AdSequence Collection of
MediaClip
mediaClipCollection
object identifiers
play( ) play( ) play( )
addClip( ) Delegates to
for all m in mediaClipCollection the play()
removeClip( ) operation in
m.play() the
getChild( ) components.
changeSequence( )
© Bennett, McRobb and Farmer 2002 20
Composite Pattern
General Form
Client Component
{Ordered}
anOperation( ) *
addComponent( )
anOperation()is removeComponent( )
polymorphically
redefined getChild( )
1
Leaf OtherLeaf Composite Collection of
Component
componentCollection
object identifiers
anOperation( ) anOperation( ) anOperation( )
addComponent( ) Delegates to
for all c in componentCollection the anOperation()
removeComponent( ) operation in
c.anOperation() the
getChild( ) components.
© Bennett, McRobb and Farmer 2002 21
Behavioural Patterns
Address the problems that arise
when assigning responsibilities to
classes and when designing
algorithms
Suggest particular static
relationships between objects and
classes and also describe how the
objects communicate
© Bennett, McRobb and Farmer 2002 22
Behavioural Patterns: State
Consider the class Campaign.
It has four states—Commissioned,
Active, Completed and Paid
A Campaign object has different
behaviour depending upon which state
it occupies
Operations have case statements giving
this alternative behaviour
The class factored into separate
components —one for each of its states
© Bennett, McRobb and Farmer 2002 23
Behavioural Patterns: State
Campaign CampaignState
currentStateIdentifier
addAdvert( ) addAdvert( )
changeState( ) calcCosts( )
Contains the object identifier
calcCosts( )
of the current state object
A:Campaign D:Campaign
C:Campaign
:Completed
:Commissioned
:Active :Paid
Context State
operation( ) operation( )
ConcreteStateA ConcreteStateB
operation( ) operation( )