Composite Design Pattern Example

Composite Design Pattern: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. --By GOF BOOK

A composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality and also shows the 1-to-many  relationship or parent child  hierarchy.

Problem: In an application if we need to represent a parent and child, node and leaf hierarchy or a tree structure. Then it is difficult to represent these structures in traditional way.

Solution: By using Composite pattern it is very easy to build these kind of hierarchy or tree structure.

This pattern is very useful, especially when you building a user interface. You can treat display object and display container object the same way, without considering the differences between the two objects. It’s very common in the GUI building environment.

Composite can be used when clients should ignore the difference between compositions of objects and individual objects. If you find that you are using multiple objects in the same way, and they often have nearly identical code to handle each of them, then composite is a good choice for you. it is less complex in this situation to treat primitives and composites as homogeneous (Node).

Lets understand it with a simple example, how to use Composite pattern in your code.

Suppose we have to represent a Company structure hierarchy  as following.
  • Company has  a CEO working for it.
  • Company has  many managers working for it.
  • Company has many Employees working for it.
A simple reporting structure could be as following.
  • Managers are working under CEO and they are reporting to him.
  • Employees are working under Managers and they report them respectively.
This reporting structure can be represented by using Composite design pattern as following.
To implement this we need to create the following Interface and Classes as genric elements.
We need to create a Node interface to represent Leaf Node (primitives) and Composite Node as homogeneous Node.

   public interface Node {
            void printNodeData();
            int getChildCount();
            Node getChild(int index);
  } class can be implemented as following, composite can have multiple nodes in it.

public class CompositeNode implements Node {
        private String nodeData;
        private ArrayList<Node> childNodes = new ArrayList<Node>();
        public CompositeNode(String nodeData) {
                 this.nodeData = nodeData;

        public void add(Node node) {

        public void remove(Node node) {

       public Node getChild(int index) {
                return childNodes.get(index);

       public int getChildCount() {
                return childNodes.size();

       public void printNodeData() {
                System.out.println("Composite data: " +nodeData);
       public String toString() {
               return nodeData;

Similarly we can implement a class, A Leaf node should not have any node in it.

No comments :

Post a Comment