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><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><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.

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]