DecoratorAdapterPattern
COMP2511
Decorator Pattern
Adapter Pattern
Prepared by
Dr. Ashesh Mahidadia
Design Patterns
vCreational Patterns
v Abstract Factory
v Factory Method
v Singleton
vStructural Patterns
v Adapter
v Composite
v Decorator
COMP2511: Decorator and Adapter Patterns 2
vBehavioral Patterns
v Iterator
v Observer
v State
v Strategy
v Template
v Visitor
This week
This week
discussed
discussed
discussed
discussed
discussed
We plan to discuss the rest of the design
patterns above in the following weeks
Decorator Pattern
3COMP2511: Decorator and Adapter Patterns
Decorator Pattern: Intent
• “Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to sub-classing for extending functionality.”
[GoF]
• Decorator design patterns allow us to selectively add functionality to an object (not the
class) at runtime, based on the requirements.
• Original class is not changed (Open-Closed Principle).
• Inheritance extends behaviors at compile time, additional functionality is bound to all the
instances of that class for their life time.
• The decorator design pattern prefers a composition over an inheritance.
Its a structural pattern, which provides a wrapper to the existing class.
• Objects can be decorated multiple times, in different order, due to the recursion involved
with this design pattern. See the example in the Demo.
• Do not need to implement all possible functionality in a single (complex) class.
COMP2511: Decorator and Adapter Patterns 4
Decorator Pattern: Structure
v Client : refers to the Component interface.
v Component: defines a common interface for
Component1 and Decorator objects
v Component1 : defines objects that get
decorated.
v Decorator: maintains a reference to a
Component object, and forwards requests to
this component object (component.operation())
v Decorator1, Decorator2, … :
Implement additional functionality
(addBehavior()) to be performed before and/or
after forwarding a request.
COMP2511: Decorator and Adapter Patterns 5
See the example in the Demo.
Decorator Pattern: Structure
v Given that the decorator has the same
supertype as the object it decorates,
we can pass around a decorated object in place
of the original (wrapped) object.
v The decorator adds its own behavior either
before and/or after delegating to the object it
decorates to do the rest of the job.
From the book “Head First Design Pattern”.
COMP2511: Decorator and Adapter Patterns 6
See the example in the Demo.
Decorator Pattern: Example
COMP2511: Decorator and Adapter Patterns 7
COMP2511: Decorator and Adapter Patterns 8
Each cost method computes the
cost of the coffee along with the
other condiments in the order
Decorator Pattern: Example
Decorator Pattern: Example
COMP2511: Decorator and Adapter Patterns 9
Decorator Pattern: Example
COMP2511: Decorator and Adapter Patterns 10
Decorator Pattern: Example
Constructing a drink order with Decorators
Decorator Pattern: Code
COMP2511: Decorator and Adapter Patterns 11
Decorator Pattern: Java I/O Example
COMP2511: Decorator and Adapter Patterns 12
Decorator Pattern: Java I/O Example
COMP2511: Decorator and Adapter Patterns 13
Decorator Pattern: Code
COMP2511: Decorator and Adapter Patterns 14
Decorator Pattern:
• Demo …
COMP2511: Decorator and Adapter Patterns 15
Adapter Pattern
16COMP2511: Decorator and Adapter Patterns
Adapter Pattern : Intent
v “Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn’t otherwise because of incompatible
interfaces.” [GoF]
v The adapter pattern allows the interface of an existing class to be used as another
interface, suitable for a client class.
v The adapter pattern is often used to make existing classes (APIs) work with a client class
without modifying their source code.
v The adapter class maps / joins functionality of two different types / interfaces.
v The adapter patter offers a wrapper around an existing useful class, such that a client
class can use functionality of the existing class.
v The adapter pattern do not offer additional functionality.
COMP2511: Decorator and Adapter Patterns 17
Adapter Pattern: Structure
v The adapter contains an instance of the class it wraps.
v In this situation, the adapter makes calls to the instance of the wrapped object.
COMP2511: Decorator and Adapter Patterns 18
Adapter: Example
COMP2511: Decorator and Adapter Patterns 19
COMP2511: Decorator and Adapter Patterns 20
Adapter: Example
Design Patterns: Discuss Differences
vCreational Patterns
v Abstract Factory
v Factory Method
v Singleton
vStructural Patterns
v Adapter
v Composite
v Decorator
COMP2511: Decorator and Adapter Patterns 21
vBehavioral Patterns
v Iterator
v Observer
v State
v Strategy
v Template
v Visitor
discussed
discussed
discussed
discussed
discussed
discussed
discussed
End
COMP2511: Decorator and Adapter Patterns 22