.NET Articles

Composite design pattern in .Net


Composite design pattern is used for managing objects in tree structure. In other words, this design pattern is used for keep object collections in parent child relationship.

Participants in this design pattern are:

Component interface

  • Declares attributes that are common in all the classes
  • Declares method to retrieve information from root to each leaf using recursive method

Leaf class

  • Implements component interface and have the information of leave level that is it has no children

Composite class

  • Implements component interface
  • It has object collection of component interface type to maintain tree structure (parent child relation)
  • It uses recursive method to traverse each leaf level

Sample Code in c#

using System;
using System.Collections.Generic;

//Component interface
public interface IEmployee
{
    //string ID { get; set; }
    string Name { get; set; }
    string Role { get; set; }
    void Display(int level);
}

//Composite class
public class Manager : IEmployee
{
    public string Name { get; set; }
    public string Role { get; set; }
    private List<IEmployee> Reportees { get; set; }
    public Manager()
    {
        Reportees = new List<IEmployee>();
    }

    public void AddReportee(IEmployee employee)
    {
        this.Reportees.Add(employee);
    }

    //Recursive method
    public void Display(int level)
    {
        Console.WriteLine(new String('-', level) + Name + "(" + Role + ")");
        foreach (IEmployee emp in Reportees)
        {
            emp.Display(level + 2);
        }
    }
}

//Leaf class
public class Programmer : IEmployee
{
    public string Name { get; set; }
    public string Role { get; set; }
    public void Display(int level)
    {
        Console.WriteLine(new String('-', level) + Name + "(" + Role + ")");
    }
}


//Client
class Program
{
    static void Main(string[] args)
    {

        //Root - Top manager
        Manager mng0_Mike_CEO = new Manager { Name = "Micheal", Role = "CEO" };

        //Manager1 with 2 reportees
        Manager mng1_Tony = new Manager { Name = "Tony", Role = "Delivery Manager" };

        Programmer Sam_prg1 = new Programmer { Name = "Sam", Role = "Junior Developer" };
        Programmer Edwin_prg2 = new Programmer { Name = "Edwin", Role = "Trainee" };

        //Add 2 programmers to manager1(Delivery Manager)
        mng1_Tony.AddReportee(Sam_prg1);
        mng1_Tony.AddReportee(Edwin_prg2);

        //Add manager1(Delivery Manager) to CEO
        mng0_Mike_CEO.AddReportee(mng1_Tony);

        //Manager 2(Architect) with 1 reportee
        Manager mng2_Rak = new Manager { Name = "Rak", Role = "Architect" };
        Programmer Chris_prg3 = new Programmer { Name = "Chris", Role = "Senior Developer" };               
        mng2_Rak.AddReportee(Chris_prg3);

        //Add manager2(Architect) to manager1(Delivery Manager)
        mng1_Tony.AddReportee(mng2_Rak);

        //Manager3(HR) with no reportees. He reports to CEO(root)
        Manager mng3_Thomas = new Manager { Name = "Thomas", Role = "HR" };
        mng0_Mike_CEO.AddReportee(mng3_Thomas);

        //Display details from Root object
        mng0_Mike_CEO.Display(1);
        Console.ReadKey();
    }
}

Output:

Composite design pattern example