Software
Engineering II
Lecture 03: Decorator Design Pattern
Ahmad Fahim Soltani
2025
Outline
Decorator design pattern
Brainstorming
Class exercise
Decorator Pattern
Go4 description: Attach additional responsibilities to an object
dynamically. Decorators provide a flexible alternative to sub-
classing for extending functionality.
4/8/2025
Decorator Pattern
The Decorator pattern is used to add non-essential behavior to
key objects in a software design.
The embellished class (or, decoratee) is wrapped up by an
arbitrary number of Decorator classes, which provide special-
case behaviors (embellishments).
Notice that the Decorator is an abstract class.
To collect the common things from all different decorators
into a base decorator class.
4/8/2025
Example: StarBuzz Coffee
Starbuzz Coffee sells different types of drinks. Each drink has a
different way of calculating its price. The drinks that Starbuzz
offers are House Blend, Dark Roast, Decaf, and Espresso.
Starbuzz needs a system to find out the price for each drink
when customers make a purchase.
4/8/2025
4/8/2025
Revisions to System
In addition to regular coffee, customers can choose different
toppings like steamed milk, soy, mocha, and whipped cream.
Starbuzz charges a little extra for each of these toppings, so
they need to include these options in their ordering system.
4/8/2025
Extensions to the Class Diagram
Add a houseBlendWithSteamedMilkandMocha subclass
Add a DarkRoastWithSteamedMilkandMocha subclass
Add a DecafWithSteamedMilkandMocha subclass
Add a EspressoWithSteamedMilkandMocha subclass
Add a HouseBlendWithSoy subclass
Add a DarkRoastWithSoy subclass
Add a DecafWithSoy subclass
Add a EspressoWithSoy subclass
...
4/8/2025
4/8/2025
Problems with Inheritance
Too many subclasses may be needed.
Need to define the implementation of each subclass.
All classes inherit a static behavior which may have to be
overridden.
The inherited behavior cannot be changed at runtime, i.e.
behavior is static at runtime.
Thus, the design is not flexible or maintainable.
4/8/2025
Another Option
Design Principle
In order to maintain code we should aim at adding new
functionality by adding new code rather than changing existing
code.
Design principle: classes should be open for extension but
closed for modification.
In this way the chances of introducing bugs or causing
unintended side effects are reduced.
Only apply this to areas that are most likely to change.
4/8/2025
Applying Decorators to the Starbuzz
Example
Order: Dark Roast with Mocha and Whip
cost() cost() cost()
Whip Mocha DarkRoast
0.1+0.2+0.99
0.99
0.2+0.99
Calculating the cost
Start with the DarkRoast Object
Wrap a Mocha object Wrap a Whip object
4/8/2025
Decorator Pattern
Provides a flexible alternative to using inheritance to extend
functionality.
This achieved by introducing decorators that “decorate”
objects.
An object can have one or more decorators.
4/8/2025
Starbuzz Coffee Example
The Decorator Pattern
The decorated object and the original object have the same
type. Thus, the decorated object can be passed instead of the
original object.
The decorator delegates part of its behaviour to the object it
decorates.
It adds its own behaviour before or after the delegation.
Objects can be delegated dynamically at runtime.
The objects are “wrapped” with decorators.
4/8/2025
Class Exercise #3
Implement the class diagram of slide17 with regarding
decorator design pattern.
4/8/2025
Beverage: Abstract Class
4/8/2025
HouseBlend: Concrete
Component
4/8/2025
DarkRoast: Concrete
Componet
4/8/2025
Espresso: Concrete
Component
4/8/2025
Decaf: Concrete Component
4/8/2025
CondimentDecorator: abstract
Decorator
4/8/2025
Whip: Decorator
4/8/2025
Soy: Decorator
4/8/2025
Mocha: Decorator
4/8/2025
Milk: Decorator
4/8/2025
StarBuzzCofee: Main Class
4/8/2025
Questions
2 mins activity