Design patterns: Composite

Hi, I’ve recently found an interesting design pattern called Composite, which is used to fold objects into a tree structure, thanks to which the client sees instead of many objects only one. And that’s the point Composite works, that client could treat many object as one. More precise about Composite further in article.



  • Creating objects in so-called tree structures.
  • Folding objects in such a way that the client sees many of them as one object.
  • Recursive composition.



You can use the composite pattern when you need to operate on the hierarchy of objects. Then you will not have to refer to each of them and you will be able to treat this hierarchy as a single object.



Referring to a group of objects as one object can be very useful in some cases, it facilitates very much the operation on the whole group, because you do not have to refer to each object in turn, you can perform the action on the whole group.



The composite, as it was said in the introduction, operates on many objects, but in sequence, below is the UML diagram of the composite pattern:

Composite scheme

The diagram shows that the composite pattern consists of the following parts:

  • Component – an abstract class or interface representing individual Leaf objects.
  • Leaf – simple type – does not have descendants.
  • Composite – stores simple objects (Leaf), implements the behavior of elements that it contains.

And of course from the client who operates on the data in the pattern.



Composite pattern schema

But it’s best to see it in a practical example, the composite looks something like this:

As you can see in the example, the Composite class has a list to which we put objects on which we want to operate, and methods that add or remove objects from the list and I also have a method that returns us the contents of this list.

Of course, we put objects to the client’s list 🙂

This is a standard scheme of composite operation, in short the composite contains a hierarchy of objects in which the main object manages its subobjects.



Real-life examples


Let’s see now in action the composite on the example of life taken. A good example is the hierarchy in a company, there are managers who manage employees.

Let’s see how it will look like in the following example:

You could expect something like this, we have a manager class that can add employees, we also have a method that displays the names of employees who are subordinates of the manager, in the client it looks like this:

It may also be that the manager may have other managers underneath who have other employees, in that case the call in the client would look like this:



Collection of figures

Let’s make an example with figures, let’s assume that we want to draw a few figures, but we do not want to deal with them individually, we only want to draw some figures and change their color on for example, red as one object. Let’s start with Component, Composite and Leaf classes.

We see that the structure looks similar to the previous example. I do not think I need to explain anything here. Now let’s see what the customer looks like:

In the client, we add figures and change the color of all added figures to the list by the draw() method.



Relations with other design patterns

  • Composite and decorator have similar schemes, because both are designed to organize a specific number of objects.
  • Flyweight is often combined with the Composite to implement shared leaf nodes.
  • The iterator can iterate through the composite. Visitor can apply operations in the composite. Composite can use the Responsibility Chain to give components global access through their parent. The composite may use the Decorator to override some parts of the composite. The composite can use the observer pattern to bind one structure to the other and the State to allow the state of the composite to change state.
  • The composite allows you to create a mediator from smaller pieces through a recursive composition.



That’s all about Composite 🙂.

As you can see, the composite is not so difficult. To sum up, the composite pattern makes it easier to operate on the hierarchy of objects, without losing access to individual objects that constitute a group, it gives the impression that we operated on a single object, in fact we operate on a group of objects.

I hope that I have explained the composite pattern well enough and that from now on you will be using it easily in your projects 🙂

Link to github with the whole code from this article:

This content also you can find on my steemit blog

And on medium:

In the next article, we will talk about the Bridge pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Notify about