Tag Archives: Composite Pattern

Composite Pattern – Design Pattern



Consider Iterator Pattern, this behaves very well if all items in a collection are equal kind, But it lacks when it comes to iterate over such a collection where all items or a few can contain a nested structure(Tree structure).

To traverse a tree, We need to visit every node(may contain further node or leaf) and leaf. So here we come of with another pattern named Composite Pattern. That works perfectly with Iterator pattern to make the job of Iteration simple for a client without knowing about underlying structure of every object in collection(Aggregate), whether it’s node or a leaf. In simple terms we can say that it will help to provide the interface that will be common for both nodes as well as leaf items, client has not to worry about object structure, that is being passed.

The Composite pattern allows you to compose objects into tree structure,to represent part-whole hierarchies. Composite lets treat individual objects and compositions of objects uniformly.

Here part-whole term is referring to each node(either a leaf or a node connected with other nodes as a parent)

Using composite structure, we can apply the same operation over both Composites(here NODEs) and individual(here LEAF) objects.

In short we are ignoring the difference between Composites(NODE) and individual(LEAF).