comp2511 decorator pattern adapter pattern
TRANSCRIPT
COMP2511
DecoratorPatternAdapterPattern
PreparedbyDr.Ashesh Mahidadia
DesignPatterns
vCreationalPatternsv AbstractFactoryv FactoryMethodv Singleton
vStructuralPatternsv Adapterv Compositev Decorator
COMP2511:DecoratorandAdapterPatterns 2
vBehavioralPatternsv Iteratorv Observerv Statev Strategyv Templatev Visitor
Thisweek
Thisweek
discussed
discussed
discussed
discussed
discussed
Weplantodiscusstherestofthedesignpatternsaboveinthefollowingweeks
DecoratorPattern
3COMP2511: Decorator and Adapter Patterns
DecoratorPattern:Intent
• "Attach additionalresponsibilities toanobjectdynamically.Decoratorsprovideaflexiblealternativetosub-classingforextendingfunctionality."[GoF]
• Decoratordesignpatternsallowustoselectivelyaddfunctionalitytoanobject(nottheclass)atruntime,basedontherequirements.
• Originalclassisnot changed(Open-ClosedPrinciple).• Inheritance extendsbehaviorsatcompiletime,additionalfunctionalityisboundtoalltheinstancesofthatclassfortheirlifetime.
• Thedecorator designpatternprefersacomposition overaninheritance.Itsastructuralpattern,whichprovidesawrapper totheexistingclass.
• Objectscanbedecorated multipletimes,indifferentorder,duetotherecursion involvedwiththisdesignpattern.SeetheexampleintheDemo.
• Donotneedtoimplementallpossiblefunctionalityinasingle(complex)class.
COMP2511:DecoratorandAdapterPatterns 4
DecoratorPattern:Structurev Client :referstotheComponentinterface.v Component:definesacommoninterfacefor
Component1 andDecorator objectsv Component1 :definesobjectsthatget
decorated.v Decorator:maintainsareferencetoa
Component object,andforwardsrequeststothiscomponentobject(component.operation())
v Decorator1,Decorator2,... :Implementadditionalfunctionality(addBehavior())tobeperformedbeforeand/orafterforwardingarequest.
COMP2511:DecoratorandAdapterPatterns 5
SeetheexampleintheDemo.
DecoratorPattern:Structurev Giventhatthedecoratorhasthesame
supertype astheobjectitdecorates,
wecanpassaroundadecorated objectinplaceoftheoriginal (wrapped)object.
v Thedecorator addsitsownbehavioreitherbeforeand/orafterdelegatingtotheobjectitdecoratestodotherestofthejob.
Fromthebook“HeadFirstDesignPattern”.
COMP2511:DecoratorandAdapterPatterns 6
SeetheexampleintheDemo.
DecoratorPattern:Example
COMP2511:DecoratorandAdapterPatterns 7
COMP2511:DecoratorandAdapterPatterns 8
Eachcostmethodcomputesthecostofthecoffeealongwiththeothercondimentsintheorder
DecoratorPattern:Example
DecoratorPattern:Example
COMP2511:DecoratorandAdapterPatterns 9
DecoratorPattern:Example
COMP2511:DecoratorandAdapterPatterns 10
DecoratorPattern:ExampleConstructingadrinkorderwithDecorators
DecoratorPattern:Code
COMP2511:DecoratorandAdapterPatterns 11
DecoratorPattern:JavaI/OExample
COMP2511:DecoratorandAdapterPatterns 12
DecoratorPattern:JavaI/OExample
COMP2511:DecoratorandAdapterPatterns 13
DecoratorPattern:Code
COMP2511:DecoratorandAdapterPatterns 14
DecoratorPattern:
• Demo…
COMP2511:DecoratorandAdapterPatterns 15
AdapterPattern
16COMP2511: Decorator and Adapter Patterns
AdapterPattern:Intent
v "Converttheinterfaceofaclassintoanotherinterfaceclientsexpect.Adapterletsclassesworktogetherthatcouldn'totherwisebecauseofincompatibleinterfaces."[GoF]
v Theadapterpatternallowstheinterface ofanexistingclasstobeusedasanotherinterface,suitablefora clientclass.
v Theadapterpatternisoftenusedtomakeexistingclasses(APIs)workwith aclient classwithoutmodifyingtheirsourcecode.
v Theadapterclassmaps/joinsfunctionalityoftwodifferenttypes/interfaces.
v Theadapterpatteroffersawrapperaroundanexistingusefulclass,suchthataclientclasscanusefunctionalityoftheexistingclass.
v Theadapterpatterndonotofferadditionalfunctionality.
COMP2511:DecoratorandAdapterPatterns 17
AdapterPattern:Structure
v Theadaptercontainsaninstanceoftheclassitwraps.v Inthissituation,theadaptermakescallstotheinstanceofthewrappedobject.
COMP2511:DecoratorandAdapterPatterns 18
Adapter:Example
COMP2511:DecoratorandAdapterPatterns 19
COMP2511:DecoratorandAdapterPatterns 20
Adapter:Example
DesignPatterns:DiscussDifferences
vCreationalPatternsv AbstractFactoryv FactoryMethodv Singleton
vStructuralPatternsv Adapterv Compositev Decorator
COMP2511:DecoratorandAdapterPatterns 21
vBehavioralPatternsv Iteratorv Observerv Statev Strategyv Templatev Visitor
discussed
discussed
discussed
discussed
discussed
discussed
discussed
End
COMP2511:DecoratorandAdapterPatterns 22