Monday, August 1, 2016

Composite Pattern


Composite pattern can be useful to setup objects in tree like structure. Composite Pattern falls under structural pattern of GOF (Gang of Four) pattern.

When to use –


Composite pattern can be used when client wants hierarchical representation of objects like tree structure where each element in tree structure can perform some task or action. This pattern can also be used when a single object and group of objects should be treated in the same way. A typical tree like structure would be company’s organization chart where CEO is at top of chart and other employees are working under him.

In composite pattern you can identify each element as either composite or leaf element. A composite element means it has some child elements. Leaf elements are elements which are at bottom and has no child element. See below tree structure of employees. The Red border elements are known as composite and has some child elements while Blue border elements are known as leaf elements and has no further elements under it.


Major components of Composite Pattern –


Component – This is an interface and provides common methods and properties to implement in Composite and Leaf classes.
Composite – This class will implement Component interface and have multiple elements under it. This class also has methods like Add, Remove, Get to perform action on child elements.
Leaf – This class will implement Component interface and can’t have elements under it.
Client – This class will create Composite and Leaf class objects and structure them as per their need.

Let’s have a look on below code which demonstrate Composite Pattern and creates hierarchical representation of employee shown above in diagram.

Code –
//Component
public interface IEmployee
{
    int EmpId { get; set; }
    string EmpName { get; set; }
    void ListAllWorkers();
}
//Composite
public class Manager : IEmployee
{
    private List<IEmployee> workerList = new List<IEmployee>();
    public int EmpId { get; set; }
    public string EmpName { get; set; }
    public void AddWorker (IEmployee emp)
    {
        workerList.Add(emp);
    }
    public void RemoveWorker (IEmployee emp)
    {
        workerList.Remove(emp);
    }
    public void ListAllWorkers()
    {
        foreach (IEmployee emp in workerList)
        {
            if (emp is Manager)
            {
                Console.WriteLine("   " + emp.EmpName);
                emp.ListAllWorkers();
            }
            if (emp is Worker)
                emp.ListAllWorkers();

        }
    }
}
//Leaf
public class Worker : IEmployee
{
    public int EmpId { get; set; }
    public string EmpName { get; set; }
    public void ListAllWorkers()
    {
        Console.WriteLine("      " + EmpName);
    }
}
//Client
class Program
{
    static void Main(string[] args)
    {
        Worker Mitesh = new Worker() { EmpId = 1, EmpName = "Mitesh" };
        Worker Jay = new Worker() { EmpId = 2, EmpName = "Jay" };
        Worker Sujata = new Worker() { EmpId = 3, EmpName = "Sujata" };
        Worker Bhavesh = new Worker() { EmpId = 4, EmpName = "Bhavesh" };
        Manager Jatin = new Manager() { EmpId = 5, EmpName = "Jatin" };
        Manager Kalpesh = new Manager() { EmpId = 6, EmpName = "Kalpesh" };
        Manager Nitin = new Manager() { EmpId = 7, EmpName = "Nitin" };

        Jatin.AddWorker(Kalpesh);
        Jatin.AddWorker(Nitin);
        Nitin.AddWorker(Sujata);
        Nitin.AddWorker(Mitesh);
        Kalpesh.AddWorker(Jay);
        Kalpesh.AddWorker(Bhavesh);

        Console.WriteLine(Jatin.EmpName);
        Jatin.ListAllWorkers();

        Console.ReadLine();
    }
}

Output –




As you can see in above example, simple hierarchy of manager and worker objects are created. You can add many worker and manager objects as you want and set them at proper hierarchy level. In above example, the Worker class acts as leaf node and doesn’t contain any child element where Manager class acts as Composite node and contains many elements under it. It also contains some more functionality to manage child object like Add/Remove/Get/Find child node.


I hope this article helps you to understand more about Composite Pattern. Please leave your feedback in comments section below.


References –

See Also –


No comments:

Post a Comment