CS计算机代考程序代写 Java flex COMP2511

COMP2511

Decorator Pattern

Prepared by
Dr. Ashesh Mahidadia

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 2

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 3

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 4

See the example in the Demo.

Decorator Pattern: Example

COMP2511: Decorator and Adapter Patterns 5

COMP2511: Decorator and Adapter Patterns 6

Each cost method computes the
cost of the coffee along with the

other condiments in the order

To
o m

an
y c

om
bin

at
ion

s

to
co

ns
ide

r!

Decorator Pattern: Example

Decorator Pattern: Example

COMP2511: Decorator and Adapter Patterns 7

Decorator Pattern: Example

COMP2511: Decorator and Adapter Patterns 8

Decorator Pattern: Example
Constructing a drink order with Decorators

Decorator Pattern: Code

COMP2511: Decorator and Adapter Patterns 9

Read the
example c

ode

discussed
/develope

d in the le
ctures,

and also p
rovided fo

r this wee
k

Decorator Pattern: Java I/O Example

COMP2511: Decorator and Adapter Patterns 10

Decorator Pattern: Java I/O Example

COMP2511: Decorator and Adapter Patterns 11

Decorator Pattern: Code

COMP2511: Decorator and Adapter Patterns 12

Read the
example c

ode

discussed
/develope

d in the le
ctures,

and also p
rovided fo

r this wee
k

Decorator Pattern:

• Demo …

COMP2511: Decorator and Adapter Patterns 13

End

COMP2511: Decorator and Adapter Patterns 14