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

Design Patterns with PlantUML: Composite Pattern

This post gives a brief overview about the Composite 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 Composite Pattern?

According to Wikipedia, the Composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to “compose” objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

What problems can the Composite design pattern solve?

  • A part-whole hierarchy should be represented so that clients can treat part and whole objects uniformly.
  • A part-whole hierarchy should be represented as tree structure.

When defining (1) Part objects and (2) Whole objects that act as containers for Part objects, clients must treat them separately, which complicates client code.

What solution does the Composite design pattern describe?

  • Define a unified Component interface for both part (Leaf) objects and whole (Composite) objects.
  • Individual Leaf objects implement the Component interface directly, and Composite objects forward requests to their child components.

This enables clients to work through the Component interface to treat Leaf and Composite objects uniformly: Leaf objects perform a request directly, and Composite objects forward the request to their child components recursively downwards the tree structure. This makes client classes easier to implement, change, test, and reuse.

UML Diagram

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

PlantUML Syntax:
<p>@startuml<br />
class Client<br />
class Component<br />
class Leaf<br />
class Composite</p>
<p>Component : operation()<br />
Component : add(Component)<br />
Component : remove(Component)<br />
Component : getChild(int)</p>
<p>Leaf : operation()</p>
<p>Composite : operation()<br />
Composite : add(Component)<br />
Composite : remove(Component)<br />
Composite : getChild(int)</p>
<p>Client -> Component<br />
Component <|– Leaf<br />
Component <|– Composite<br />
Component “0..*” <–o “1” Composite</p>
<p>note top of Client<br />
The Client uses the<br />
Component interface to<br />
manipulate the objects in the<br />
composition.<br />
end note</p>
<p>note top of Component<br />
The Component defines an<br />
interface for all objects in<br />
the composition: both the<br />
composite and the leaf nodes.<br />
end note</p>
<p>note top of Component<br />
The Component may implement a<br />
default behavior for add(), remove(),<br />
getChild() and its operations.<br />
end note</p>
<p>note bottom of Leaf<br />
A Leaf has no<br />
children.<br />
end note</p>
<p>note left of Leaf<br />
Note that the Leaf also<br />
inherits methods like add(),<br />
remove() and getChild(), which<br />
do not necessarily make a lot of<br />
sense for a leaf node. We are<br />
going to come back to this issue.<br />
end note</p>
<p>note bottom of Leaf<br />
A Leaf defines the behavior for the<br />
elements in the composition. It does<br />
this by implementing the operations<br />
the Composite supports.<br />
end note</p>
<p>note bottom of Composite<br />
The Composite’s role is to define<br />
behavior of the components<br />
having children and to store child<br />
components.<br />
end note</p>
<p>note right of Composite<br />
The Composite also<br />
implements the Leaf-<br />
related operations.<br />
Note that some of<br />
these may not make<br />
sense on a Composite,<br />
so in that case an<br />
exception might be<br />
generated.<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
class Client
class Component
class Leaf
class Composite

Component : operation()
Component : add(Component)
Component : remove(Component)
Component : getChild(int)

Leaf : operation()

Composite : operation()
Composite : add(Component)
Composite : remove(Component)
Composite : getChild(int)

Client -> Component
Component <|-- Leaf
Component <|-- Composite
Component "0..*" <--o "1" Composite

@enduml

Other Design Patterns

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

Proxy Pattern Design Pattern with PlantUML: Proxy Pattern - This post gives a brief overview about the Proxy Pattern. The post is part of a series about software design ... Read More
State Pattern Design Patterns with PlantUML: State Pattern - This post gives a brief overview about the State Pattern. The post is part of a series about software design ... Read More
Composite Pattern Design Patterns with PlantUML: Composite Pattern - This post gives a brief overview about the Composite Pattern. The post is part of a series about software design ... Read More
Iterator Pattern Design Patterns with PlantUML: Iterator Pattern - This post gives a brief overview about the Iterator Pattern. The post is part of a series about software design ... Read More
Template Pattern Design Patterns with PlantUML: Template Pattern - This post gives a brief overview about the Template Pattern. The post is part of a series about software design ... Read More
Facade Pattern Design Patterns with PlantUML: Facade Pattern - This post gives a brief overview about the Facade Pattern. The post is part of a series about software design ... Read More
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

Check Also

Proxy Pattern

Design Pattern with PlantUML: Proxy Pattern

This post gives a brief overview about the Proxy 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.