comp2511 decorator pattern adapter pattern

22
COMP2511 Decorator Pattern Adapter Pattern Prepared by Dr. Ashesh Mahidadia

Upload: others

Post on 10-Apr-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP2511 Decorator Pattern Adapter Pattern

COMP2511

DecoratorPatternAdapterPattern

PreparedbyDr.Ashesh Mahidadia

Page 2: COMP2511 Decorator Pattern Adapter Pattern

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

Page 3: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern

3COMP2511: Decorator and Adapter Patterns

Page 4: COMP2511 Decorator Pattern Adapter Pattern

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

Page 5: COMP2511 Decorator Pattern Adapter Pattern

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.

Page 6: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:Structurev Giventhatthedecoratorhasthesame

supertype astheobjectitdecorates,

wecanpassaroundadecorated objectinplaceoftheoriginal (wrapped)object.

v Thedecorator addsitsownbehavioreitherbeforeand/orafterdelegatingtotheobjectitdecoratestodotherestofthejob.

Fromthebook“HeadFirstDesignPattern”.

COMP2511:DecoratorandAdapterPatterns 6

SeetheexampleintheDemo.

Page 7: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:Example

COMP2511:DecoratorandAdapterPatterns 7

Page 8: COMP2511 Decorator Pattern Adapter Pattern

COMP2511:DecoratorandAdapterPatterns 8

Eachcostmethodcomputesthecostofthecoffeealongwiththeothercondimentsintheorder

DecoratorPattern:Example

Page 9: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:Example

COMP2511:DecoratorandAdapterPatterns 9

Page 10: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:Example

COMP2511:DecoratorandAdapterPatterns 10

DecoratorPattern:ExampleConstructingadrinkorderwithDecorators

Page 11: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:Code

COMP2511:DecoratorandAdapterPatterns 11

Page 12: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:JavaI/OExample

COMP2511:DecoratorandAdapterPatterns 12

Page 13: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:JavaI/OExample

COMP2511:DecoratorandAdapterPatterns 13

Page 14: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:Code

COMP2511:DecoratorandAdapterPatterns 14

Page 15: COMP2511 Decorator Pattern Adapter Pattern

DecoratorPattern:

• Demo…

COMP2511:DecoratorandAdapterPatterns 15

Page 16: COMP2511 Decorator Pattern Adapter Pattern

AdapterPattern

16COMP2511: Decorator and Adapter Patterns

Page 17: COMP2511 Decorator Pattern Adapter Pattern

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

Page 18: COMP2511 Decorator Pattern Adapter Pattern

AdapterPattern:Structure

v Theadaptercontainsaninstanceoftheclassitwraps.v Inthissituation,theadaptermakescallstotheinstanceofthewrappedobject.

COMP2511:DecoratorandAdapterPatterns 18

Page 19: COMP2511 Decorator Pattern Adapter Pattern

Adapter:Example

COMP2511:DecoratorandAdapterPatterns 19

Page 20: COMP2511 Decorator Pattern Adapter Pattern

COMP2511:DecoratorandAdapterPatterns 20

Adapter:Example

Page 21: COMP2511 Decorator Pattern Adapter Pattern

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

Page 22: COMP2511 Decorator Pattern Adapter Pattern

End

COMP2511:DecoratorandAdapterPatterns 22