You are on page 1of 7

Decorator Pattern

A Decorator
Allows dynamic allocation of new behaviours
within the object - at runtime

2
When to use it:

⬡ For avoiding “Class Explosion”


⬡ When altering the behaviour of an object
without physically changing the class
⬡ To modify the behaviour of only one object in a
class

3
Advantages/Disadvantages

Advantage : Runtime execution


(inheritance at compile time)

Disadvantage: Use of similar kinds of objects

4
Implementation
1. Define decorator object that implement an interface of the extended
(decorated) object (component) by forwarding all requests to it
2. Perform the additional functionality before/after forwarding the request

5
Examples of use:

⬡ In most Java IO classes : FileReader, BufferReader


⬡ Window Class, Horizontal and Vertical scroll as decorators
⬡ Coffee shop Ex.

6
Thanks!
Any questions?

You might also like