The Decorator Design Pattern
Decorating our classes at runtime
using a form of object composition
1
Decorator Pattern
A structural design pattern that lets you attach new behaviors to
objects by placing these objects inside special wrapper objects that
contain the behaviors.
Decorator Pattern
Welcome to our coffee
They first designed their classes like this… However,
Class explosion due to inheritance
Each class has to write its own cost() method….
5
Beverage class add instance variables to represent whether or not
each beverage has milk, soy, mocha and whip... 6
8
Decorator Pattern
Our goal is to allow classes to be easily extended to
incorporate new behavior without modifying existing code.
What do we get if we accomplish this?
Designs that are flexible to change and flexible enough to
take on new functionality to meet changing requirements.
9
Constructing a drink order with Decorators
Constructing a drink order with Decorators
Computing the cost
We do this by calling cost() on the outermost decorator, Whip, and
Whip is going to delegate computing the cost to the objects it
decorates.
Once it gets a cost, it will add on the cost of the Whip.
The decorator pattern
13
Decorator Pattern
14
Decorating our beverages
Code of the Decorator
16
Coding beverages
17
Coding condiments (tastes)
18
Running the code
19
Another Example
a Shape interface and concrete classes implementing the Shape interface.
create an abstract decorator class ShapeDecorator implementing the Shape interface
and having Shape object as its instance variable.
RedShapeDecorator is concrete class implementing ShapeDecorator.
20
Demo class will use RedShapeDecorator to decorate Shape objects.
Example
21
Example
22
Example
23
Example
24
Decorator Pattern
Pros
Extending an object’s behavior without making a new subclass.
• You can add or remove responsibilities from an object at runtime.
• You can combine several behaviors by wrapping an object into
multiple decorators.
• Single Responsibility Principle. You can divide a monolithic class that
implements many possible variants of behavior into several smaller
classes.
Cons
• Hard to remove a specific wrapper from the wrappers stack.
• Hard to implement a decorator in such a way that its behavior
doesn’t depend on the order in the decorators stack.
Class diagram for Decorator Pattern
(unity example)
When to use the Decorator pattern
• We need to attach multiple attachments to a weapon.
• We need to be able to add and remove them at runtime.
27
Attachments that the player could buy
Injector: amplifies the weapon's
damage capacity.
Stabilizer: reduces vibrations caused
when the bike hits its top speed.
Cooler: It enhances the rate of fire
and reduces the cool-down duration.
Implementing the weapon system
BikeWeapon class
BikeWeapon class
BikeWeapon class
31
Weapon class
This is the object we will
decorate with attachments.
32
Testing the weapon system
Screenshot of the code example in action inside Unity
35
BikeWeapon component properties
36
37