Home » Blog » Software Development » Software Design Patterns in PlantUML » Design Patterns with PlantUML: Decorator Pattern

Design Patterns with PlantUML: Decorator Pattern

This post gives a brief overview about the Decorator Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML. The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Decorator Pattern?

According to Wikipedia, the decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern.

UML Diagram

The following diagram shows the Decorator Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:
<p>@startuml<br />
skinparam componentStyle uml2</p>
<p>class Component<br />
class ConcreteComponent<br />
class Decorator<br />
class ConcreteDecoratorA<br />
class ConcreteDecoratorB</p>
<p>Component : methodA()<br />
Component : methodB()<br />
Component : // otherMethods()</p>
<p>ConcreteComponent : methodA()<br />
ConcreteComponent : methodB()<br />
ConcreteComponent : // otherMethods()</p>
<p>Decorator : methodA()<br />
Decorator : methodB()<br />
Decorator : // otherMethods()</p>
<p>ConcreteDecoratorA : Component wrappedObject<br />
ConcreteDecoratorA : methodA()<br />
ConcreteDecoratorA : methodB()<br />
ConcreteDecoratorA : newBehavior()<br />
ConcreteDecoratorA : // otherMethods()</p>
<p>ConcreteDecoratorB : Component wrappedObject<br />
ConcreteDecoratorB : Object newState<br />
ConcreteDecoratorB : methodA()<br />
ConcreteDecoratorB : methodB()<br />
ConcreteDecoratorB : // otherMethods()</p>
<p>Component <|– ConcreteComponent<br />
Component <|– Decorator<br />
Decorator <|– ConcreteDecoratorA<br />
Decorator <|– ConcreteDecoratorB<br />
Decorator –> Component : component<br />
note right on link<br />
Each component can be used on its<br />
own, or wrapped by a decorator<br />
component<br />
end note</p>
<p>note bottom of ConcreteComponent<br />
The ConreteComponent<br />
is the object we are going<br />
to dynamically add new<br />
behavior to it. It extends<br />
Component.<br />
end note</p>
<p>note bottom of Decorator<br />
Decorators implement the<br />
same interface or abstract<br />
class as the component they<br />
are going to decorate.<br />
end note</p>
<p>note bottom of ConcreteDecoratorB<br />
Decorators can extend the<br />
state of the component<br />
end note</p>
<p>note bottom of ConcreteDecoratorB<br />
Decorators can add new methods;<br />
however, new behavior is typically<br />
added by doing computation<br />
before or after an existing method<br />
in the component.<br />
end note</p>
<p>note bottom of ConcreteDecoratorA<br />
The ConcreteDecorator has an<br />
instance variable for the thing<br />
it decorates (the Component the<br />
Decorator wraps).<br />
end note<br />
@enduml</p>

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:


@startuml
skinparam componentStyle uml2

class Component
class ConcreteComponent
class Decorator
class ConcreteDecoratorA
class ConcreteDecoratorB

Component : methodA()
Component : methodB()
Component : // otherMethods()

ConcreteComponent : methodA()
ConcreteComponent : methodB()
ConcreteComponent : // otherMethods()

Decorator : methodA()
Decorator : methodB()
Decorator : // otherMethods()

ConcreteDecoratorA : Component wrappedObject
ConcreteDecoratorA : methodA()
ConcreteDecoratorA : methodB()
ConcreteDecoratorA : newBehavior()
ConcreteDecoratorA : // otherMethods()

ConcreteDecoratorB : Component wrappedObject
ConcreteDecoratorB : Object newState
ConcreteDecoratorB : methodA()
ConcreteDecoratorB : methodB()
ConcreteDecoratorB : // otherMethods()

Component <|-- ConcreteComponent
Component <|-- Decorator
Decorator <|-- ConcreteDecoratorA
Decorator <|-- ConcreteDecoratorB
Decorator --> Component : component

@enduml

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

Adapter Pattern Design Patterns with PlantUML: Adapter Pattern - This post gives a brief overview about the Adapter Pattern. The post is part of a series about software design ... Read More
Command Pattern Design Patterns with PlantUML: Command Pattern - This post gives a brief overview about the Command Pattern. The post is part of a series about software design ... Read More
Singleton Design Patterns with PlantUML: Singleton - This post gives a brief overview about the Singleton Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Abstract Factory Pattern - This post gives a brief overview about the Abstract Factory Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Factory Method Pattern - This post gives a brief overview about the Factory Method Pattern. The post is part of a series about software ... Read More
Decorator Pattern Design Patterns with PlantUML: Decorator Pattern - This post gives a brief overview about the Decorator Pattern. The post is part of a series about software design ... Read More
Observer Pattern Design Patterns with PlantUML: Observer Pattern - This post gives a brief overview about the Observer Pattern. The post is part of a series about software design ... Read More

Check Also

Adapter Pattern

Design Patterns with PlantUML: Adapter Pattern

This post gives a brief overview about the Adapter Pattern. The post is part of …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

We use Cookies and similar technology to collect and analyse information about the users of this website. We use this information to enhance the content, advertising and other services available on the site. Please click ‘Accept cookies’ to consent to the use of this technology by petrockblock. You can manage your preferences at any time by visiting our Cookies Policy page.