Structural Design Patterns in Java: Composite Design Pattern

Abdulaziz Pulatjonov
DevOps.dev
Published in
3 min readJun 3, 2022

--

The Composite Design Pattern suggests allowing to treat of individual objects and compositions of objects in the same way. In other words, methods consider these objects' individual objects and compositions in the same behavior without knowing it.

The Tree Structure could be the perfect example of this design pattern as it inherits a base type and can be represented as a part or a whole hierarchy of objects.

The Composite Design Pattern can be divided into 4 parts:

  1. Component — either interface or an abstract class with the base methods implemented in child composites. It is considered the base parent for all the composition objects.
  2. Leaf — object which implements the behavior of the Component part and does not contain any reference to other objects.
  3. Composite — object that implements the behavior of the Component part and contains references to other composite objects. In other words, it is just like the Leaf element but the only difference is its references to other Composite or Leaf objects.
  4. Client — an instance of the object which has access to the composition elements by using the base Component object.

Let’s consider the condition where we want to create the hierarchy of the plant kingdom to represent an apple and rose example.

First, we will create a Base Component for the Plant Kingdom:

And now we will create Leaf elements for apple and rose:

Both classes implement the base Component interface’s “getName()” method. And they do not contain any reference for other components as they are Leaf elements.

For the Composite components let’s create a class that represents the family of the Leaf elements. Since both apple and rose share the same family in the plant kingdom which is “Rosaceae”, we will create a Composite family class for them:

In the above code, you can see the example for the Composite class as it holds the collection of Plant objects and some of its own methods to add and delete the elements of that collection.

Implementation of the Plant base Component is accomplished by iterating through elements of the collection of leaf elements.

And now testing below will pass successfully:

In the above code, in the first two tests, we created objects of the Leaf elements and checked for their “getName” methods, and in the last test, we checked the Composite element’s “getName” method alongside with implementation of the “getName()” method.

Rosaceae object created on the basis of the Rosaceae class as we could create an object on the basis of Base Component as well, however, in that case, Rosaceae’s own methods could not be callable.

The main advantage of the Composite Design Pattern is that client does not have to worry about whether an object is a simple object or a collection of that objects.

The code is available in the Github project.

There are 6 more Structural Design Patterns that are:

  1. Adapter Design Pattern
  2. Bridge Design Pattern
  3. Decorator Design Pattern
  4. Facade Design Pattern
  5. Flyweight Design Pattern
  6. Proxy Design Pattern

--

--

BS in Business Information Systems. Interests: Computer Science, Engineering, Public speaking, Languages, Music, Reading, Writing and so on