Click here to Skip to main content
14,265,748 members

Composite Design Pattern

Rate this:
4.77 (23 votes)
Please Sign up or sign in to vote.
4.77 (23 votes)
4 May 2011CPOL
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)
    { = 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)
    { = 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)

    public void AddSubordinate(IEmployee employee)

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


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


About the Author

United States United States
No Biography provided

Comments and Discussions

PraiseCan finally understand Composite Design Pattern Pin
Rishab_15-Sep-16 18:58
professionalRishab_15-Sep-16 18:58 
Questiontank you a lot Pin
Member 1112534729-Jul-15 21:30
memberMember 1112534729-Jul-15 21:30 
GeneralVery good article, needs little polishing Pin
Malhotra Sameer14-Jul-15 1:05
professionalMalhotra Sameer14-Jul-15 1:05 
GeneralRe: Very good article, needs little polishing Pin
Member 1231265028-Nov-16 7:55
memberMember 1231265028-Nov-16 7:55 
GeneralGreat! Pin
raananv2-Apr-14 3:19
memberraananv2-Apr-14 3:19 
GeneralMy vote of 5 Pin
GenadyT23-Mar-14 4:57
professionalGenadyT23-Mar-14 4:57 
GeneralTake 5 Pin
sobo12312-Nov-13 7:26
membersobo12312-Nov-13 7:26 
QuestionIs it possible to disable leaf ? Pin
DevMec5-Dec-12 22:31
memberDevMec5-Dec-12 22:31 
AnswerRe: Is it possible to disable leaf ? Pin
shanAtCP20-May-14 2:43
membershanAtCP20-May-14 2:43 
GeneralMy vote of 5 Pin
sayooj cyriac24-Oct-12 4:24
membersayooj cyriac24-Oct-12 4:24 
SuggestionAnother example of Composite Design Pattern Pin
Nilesh Gule8-Aug-12 6:09
memberNilesh Gule8-Aug-12 6:09 
GeneralNice job... Pin
Paul Selormey24-Apr-11 15:46
memberPaul Selormey24-Apr-11 15:46 
GeneralRe: Nice job... Pin
Member 1112534729-Jul-15 21:24
memberMember 1112534729-Jul-15 21:24 

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

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

Technical Blog
Posted 23 Apr 2011

Tagged as


42 bookmarked