0% found this document useful (0 votes)
69 views

Decorator Pattern

The Decorator Pattern allows dynamic allocation of new behaviors to objects at runtime by attaching additional responsibilities to an individual object without affecting the behavior of other objects. It involves defining a decorator class that implements the same interface as the component it decorates and contains it as a field, forwarding requests to the contained component while adding extra functionality before and/or after. This allows behaviors to be added and removed at runtime and avoids class explosion.

Uploaded by

Nefer Nari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Decorator Pattern

The Decorator Pattern allows dynamic allocation of new behaviors to objects at runtime by attaching additional responsibilities to an individual object without affecting the behavior of other objects. It involves defining a decorator class that implements the same interface as the component it decorates and contains it as a field, forwarding requests to the contained component while adding extra functionality before and/or after. This allows behaviors to be added and removed at runtime and avoids class explosion.

Uploaded by

Nefer Nari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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