Professional Documents
Culture Documents
Lethbridge/
Chapter6:Usingdesignpattern
Pattern description
Context:
Thegeneralsituationinwhichthepatternapplies
Problem:
Ashortsentenceortworaisingthemaindifficulty.
Forces:
Theissuesorconcernstoconsiderwhensolvingtheproblem
Solution:
Therecommendedwaytosolvetheprobleminthegivencontext.
tobalancetheforces
Antipatterns:(Optional)
Solutionsthatareinferiorordonotworkinthiscontext.
Relatedpatterns:(Optional)
Patternsthataresimilartothispattern.
References:
Whodevelopedorinspiredthepattern.
Lethbridge/
Chapter6:Usingdesignpattern
Problem:
Whatisthebestwaytorepresentsuchsetsofoccurrencesina
classdiagram?
Forces:
Youwanttorepresentthemembersofeachsetofoccurrences
withoutduplicatingthecommoninformation
Lethbridge/
Chapter6:Usingdesignpattern
Abstraction-Occurrence
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Abstraction-Occurrence
Antipatterns:
Lethbridge/
Chapter6:Usingdesignpattern
Abstraction-Occurrence
Squarevariant
Lethbridge/
Chapter6:Usingdesignpattern
Someobjectscannothaveanysubordinates
Problem:
How do you represent a hierarchy of objects, in which some
objectscannothavesubordinates?
Forces:
Youwantaflexiblewayofrepresentingthehierarchy
thatpreventscertainobjectsfromhavingsubordinates
Alltheobjectshavemanycommonpropertiesandoperations
Lethbridge/
Chapter6:Usingdesignpattern
General Hierarchy
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
General Hierarchy
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
General Hierarchy
Antipattern:
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Player-Role
Forces:
Itisdesirabletoimproveencapsulationbycapturingthe
informationassociatedwitheachseparateroleinaclass.
Youwanttoavoidmultipleinheritance.
Youcannotallowaninstancetochangeclass
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Player-Role
Example1:
Lethbridge/
Chapter6:Usingdesignpattern
Player-Role
Example2:
Lethbridge/
Chapter6:Usingdesignpattern
Player-Role
Antipatterns:
Merge all the properties and behaviours into a single
PlayerclassandnothaveRoleclassesatall.
CreaterolesassubclassesofthePlayerclass.
Lethbridge/
Chapter6:Usingdesignpattern
Chapter6:Usingdesignpattern
Singleton
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Chapter6:Usingdesignpattern
Observer
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Observer
Antipatterns:
Connect an observer directly to an observable so that
theybothhavereferencestoeachother.
Maketheobserverssubclassesoftheobservable.
Lethbridge/
Chapter6:Usingdesignpattern
Problem:
Howcanyoumosteffectivelymakeuseofamethod
thatalreadyexistsintheotherclass?
Forces:
Youwanttominimizedevelopmentcostbyreusing
methods
Lethbridge/
Chapter6:Usingdesignpattern
Delegation
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Delegation
Example:
Lethbridge/
Chapter6:Usingdesignpattern
Delegation
Antipatterns
Overusegeneralizationandinheritthemethodthatisto
bereused
InsteadofcreatingasinglemethodintheDelegator
thatdoesnothingotherthancallamethodinthe
Delegate
considerhavingmanydifferentmethodsinthe
Delegatorcallthedelegatesmethod
Accessnonneighboringclasses
returnspecificFlight.regularFlight.flightNumber();
returngetRegularFlight().flightNumber();
Lethbridge/
Chapter6:Usingdesignpattern
astheothermethodsinthehierarchy?
Forces:
You do not have access to multiple inheritance or you do not
wanttouseit.
Lethbridge/
Chapter6:Usingdesignpattern
Adapter
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Adapter
Example:
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Faade
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Read-only Interface
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Read-only Interface
Example:
Lethbridge/
Chapter6:Usingdesignpattern
Read-only Interface
Antipatterns:
MakethereadonlyclassasubclassoftheMutableclass
Overrideallmethodsthatmodifyproperties
suchthattheythrowanexception
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Proxy
Solution:
Lethbridge/
Chapter6:Usingdesignpattern
Proxy
Examples:
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
3.Definetheupdate methodinthenewclass:
public void update(Observable obs, Object message)
{
if (message instanceOf SomeClass)
{
// process the message
}
}
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern
Lethbridge/
Chapter6:Usingdesignpattern