Decorator pattern
The Decorator Pattern allows new functionality to be added to an existing object without changing its structure. This type of design pattern belongs to the structural patterns, and it acts as a wrapper around an existing class.
The Decorator Pattern modifies the behavior of an object dynamically by wrapping it in a decorator class.
This pattern creates a decorator class to wrap the original class and, while keeping the class method signatures intact, provides extra functionality.
We demonstrate the use of the Decorator Pattern with the example below. In it, we will decorate a shape with different colors without changing the shape class.
Summary
Intent
Dynamically add extra responsibilities to an object without changing its structure. The Decorator Pattern provides a flexible alternative to inheritance for extending functionality.
Main problem solved
- Avoid introducing static features through inheritance, especially when the number of subclasses grows rapidly.
- Allow the functionality of an object to be added or modified dynamically at runtime.
Use Cases
- When you need to extend the functionality of a class without adding a large number of subclasses.
- When you need to add or remove the functionality of an object dynamically.
Implementation
- Define the Component interface: create an interface that specifies the standard for objects to which responsibilities can be added dynamically.
- Create the Concrete Component: a concrete class implementing this interface that provides the basic functionality.
- Create the Abstract Decorator: implements the same interface, holds a reference to a Component interface, and can add functionality dynamically at any time.
- Create the Concrete Decorator: extends the Abstract Decorator and adds extra responsibilities.
Key code
- Component interface: defines the standard for objects that can be decorated.
- ConcreteComponent class: a concrete class implementing the Component interface.
- Decorator abstract class: implements the Component interface and contains a reference to a Component interface.
- ConcreteDecorator class: extends the Decorator class and adds extra functionality.
Application Example
- Sun Wukong's 72 Transformations: Sun Wukong (ConcreteComponent) gains new abilities through transformations (Decorator).
- Framed painting: a painting (ConcreteComponent) can be enhanced for display by adding glass (ConcreteDecorator) and a frame (ConcreteDecorator).
Advantages
- Low coupling: the decorator class and the decorated class can change independently without affecting each other.
- Flexibility: functionality can be added or removed dynamically.
- Alternative to inheritance: provides a way to extend object functionality other than inheritance.
Disadvantages
- Complexity: multiple layers of decoration may increase system complexity.
Usage recommendation
- When you need to extend functionality dynamically, consider using the Decorator Pattern.
- Keep the interfaces of the decorator and the concrete component consistent to ensure flexibility.
Notes
- The Decorator Pattern can replace inheritance, but it should be used carefully to avoid excessive decoration that makes the system complex.
structures
The Decorator Pattern contains the following core roles:
- Abstract Component (Component): defines the common interface or abstract class for the original object and the decorator objects; it can be the parent class or interface of the concrete component class.
- Concrete Component: the original object being decorated; it defines the object to which new functionality needs to be added.
- Abstract Decorator (Decorator): inherits from the Abstract Component, contains an Abstract Component object, and defines the same interface as the Abstract Component, while it can also hold other decorator objects through composition.
- Concrete Decorator: implements the interface of the Abstract Decorator and is responsible for adding new functionality to the Abstract Component. A Concrete Decorator usually performs its own operations before or after calling the original object's methods.
By nesting and wrapping multiple decorator objects, the Decorator Pattern can achieve multi-level functional enhancement. Each concrete decorator class can selectively add new functionality while keeping the object interface consistent.
Create a class that implements
We will create a Shape interface as the bridge implementation and concrete classes that implement the Shape entity class that implements the Shape interface. Then we create an abstract decorator class that implements the ShapeDecoratorinterface, and takes the Shape object as its instance variable.
RedShapeDecorator is the entity class that implements ShapeDecorator .
DecoratorPatternDemo Class uses RedShapeDecorator to decorate Shape object.

Step 1
Create an interface:
Shape.java
public
interface
Shape
{
void
draw
(
)
;
}
Step 2
Create concrete classes that implement the interface.
Rectangle.java
public
class
Rectangle
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Shape: Rectangle
"
)
;
}
}
Circle.java
public
class
Circle
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Shape: Circle
"
)
;
}
}
Step 3
Create concrete classes that implement the Shape the abstract decorator class for the interface.
ShapeDecorator.java
public
abstract
class
ShapeDecorator
implements
Shape
{
protected
Shape
decoratedShape
;
public
ShapeDecorator
(
Shape
decoratedShape
)
{
this
.
decoratedShape
=
decoratedShape
;
}
public
void
draw
(
)
{
decoratedShape
.
draw
(
)
;
}
}
Step 4
Create an entity decorator class that extends the ShapeDecorator class.
RedShapeDecorator.java
public
class
RedShapeDecorator
extends
ShapeDecorator
{
public
RedShapeDecorator
(
Shape
decoratedShape
)
{
super
(
decoratedShape
)
;
}
@
Override
public
void
draw
(
)
{
decoratedShape
.
draw
(
)
;
setRedBorder
(
decoratedShape
)
;
}
private
void
setRedBorder
(
Shape
decoratedShape
)
{
System
.
out
.
println
(
"
Border Color: Red
"
)
;
}
}
Step 5
Use RedShapeDecorator to decorate Shape object.
DecoratorPatternDemo.java
public
class
DecoratorPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Shape
circle
=
new
Circle
(
)
;
ShapeDecorator
redCircle
=
new
RedShapeDecorator
(
new
Circle
(
)
)
;
ShapeDecorator
redRectangle
=
new
RedShapeDecorator
(
new
Rectangle
(
)
)
;
//
Shape redCircle = new RedShapeDecorator(new Circle());
//
Shape redRectangle = new RedShapeDecorator(new Rectangle());
System
.
out
.
println
(
"
Circle with normal border
"
)
;
circle
.
draw
(
)
;
System
.
out
.
println
(
"
\n
Circle of red border
"
)
;
redCircle
.
draw
(
)
;
System
.
out
.
println
(
"
\n
Rectangle of red border
"
)
;
redRectangle
.
draw
(
)
;
}
}
Step 6
Run the program, output result:
Circle with normal border
Shape: Circle
Circle of red border
Shape: Circle
Border Color: Red
Rectangle of red border
Shape: Rectangle
Border Color: Red