Click here to Skip to main content
Click here to Skip to main content
Go to top

Composite Design Pattern

, 4 May 2011
Rate this:
Please Sign up or sign in to vote.
Composite Design Pattern Clearly Explained with Example

You can see this and other great articles on design patterns here.

The composite design pattern allows you to set up a tree structure and ask each element in the tree structure to perform a task. A typical tree structure would be a company organization chart, where the CEO is at the top and other employees at the bottom. After the tree structure is established, you can then ask each element, or employee, to perform a common operation.

The composite pattern classifies each element in the tree as a composite or a leaf. A composite means that there can be other elements below it, whereas a leaf cannot have any elements below it. Therefore the leaf must be at the very bottom of the tree. The concept is shown in the diagram below:  

Let's take a look at the UML of the composite pattern first, then we will do an example to see how it works. Below is the UML of the Composite Design Pattern, where you see the distinction between a composite element and the leaf:

  • The IComponent interface defines the methods that both the Composite class and the Leaf class must implement. The Operation method is the common method that all elements in the tree structure can perform. The IComponent simply represents an element in the tree.
  • The Leaf class are elements that cannot have any elements below it, and it only has Operation method to perform the task for the element.
  • The Composite class are elements that can have 0 or more elements below it. The methods that it supports are as follows:
    • The AddComponent method adds an element below it
    • The GetChild method gets all the elements below it
    • The Operation method performs the task for the element itself
    • The RemoveComponent method deletes an element below it

Let's do an example to see how it works. In a company, we have supervisors and workers. The supervisors can manage other supervisors or workers under them. The supervisors will be the composites. The workers do not manage anyone and they will be the leaves.

All the supervisors and workers are employees, and as an employee you can always show your happiness level in the company (this is the common operation of the elements). The UML for this example is shown below:

  • The IEmployee interface defines the operation that all employees must be able to perform, which is the ShowHappiness method.
  • The Worker class are the employees that do not manage anyone, and implements only the ShowHappiness method.
  • The Supervisor class are the employees that can manage other employees and have the following variables and methods:
    • The private variable subordinate are the list of employees that the supervisor manages.
    • The AddSubordinate method adds an employee under the supervisor.
    • The ShowHappiness method shows the supervisor's happiness level.

When you call a supervisor's ShowHappiness method, it will show both the supervisor’s happiness and all of its subordinate’s happiness by calling each of the subordinate's ShowHappiness method.

The key to the composite design pattern is that it allows you to set up a structure with a common operation (such as the ShowHappiness method), and then you can have all the elements to perform the common operation. This is done by keeping a list of child elements that implements the common interface in the composite class, and then calling each child element's operations.

 

Below are the implementation code and the output for our example. Notice that you can add any number of supervisors at any level of the organization and the composite will show the happiness for everyone under the composite:

class Program
{
    static void Main(string[] args)
    {
        Worker a = new Worker("Worker Tom", 5);
        Supervisor b = new Supervisor("Supervisor Mary", 6);
        Supervisor c = new Supervisor("Supervisor Jerry", 7);
        Supervisor d = new Supervisor("Supervisor Bob", 9);
        Worker e = new Worker("Worker Jimmy", 8);

        //set up the relationships
        b.AddSubordinate(a); //Tom works for Mary
        c.AddSubordinate(b); //Mary works for Jerry
        c.AddSubordinate(d); //Bob works for Jerry
        d.AddSubordinate(e); //Jimmy works for Bob

        //Jerry shows his happiness and asks everyone else to do the same
        if (c is IEmployee)
            (c as IEmployee).ShowHappiness();
    }
}

public interface IEmployee
{
    void ShowHappiness();
}

public class Worker : IEmployee
{
    private string name;
    private int happiness;

    public Worker(string name, int happiness)
    {
        this.name = name;
        this.happiness = happiness;
    }

    void IEmployee.ShowHappiness()
    {
        Console.WriteLine(name + " showed happiness level of " + happiness);
    }
}

public class Supervisor : IEmployee
{
    private string name;
    private int happiness;

    private List<iemployee> subordinate = new List<iemployee>();

    public Supervisor(string name, int happiness)
    {
        this.name = name;
        this.happiness = happiness;
    }

    void IEmployee.ShowHappiness()
    {
        Console.WriteLine(name + " showed happiness level of " + happiness);
        //show all the subordinate's happiness level
        foreach (IEmployee i in subordinate)
            i.ShowHappiness();
    }

    public void AddSubordinate(IEmployee employee)
    {
        subordinate.Add(employee);
    }
}

Liked this article? You can see this and other great articles on design patterns here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
SuggestionAnother example of Composite Design Pattern PinmemberNilesh Gule8-Aug-12 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid