Pages

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

publicinterfaceIEmployee

{

int EmpId { get; set; }

string EmpName { get; set; }

void ListAllWorkers();

}

//Composite

publicclassManager : IEmployee

{

privateList<IEmployee>
workerList = newList<IEmployee>();

publicint EmpId {
get; set; }

publicstring EmpName
{ get; set; }

publicvoid
AddWorker (IEmployee emp)

{

workerList.Add(emp);

}

publicvoid
RemoveWorker (IEmployee emp)

{

workerList.Remove(emp);

}

publicvoid
ListAllWorkers()

{

foreach (IEmployee emp in
workerList)

{

if (emp isManager)

{

Console.WriteLine(" " + emp.EmpName);

emp.ListAllWorkers();

}

if (emp isWorker)

emp.ListAllWorkers();

}

}

}

//Leaf

publicclassWorker : IEmployee

{

publicint EmpId {
get; set; }

publicstring EmpName
{ get; set; }

publicvoid
ListAllWorkers()

{

Console.WriteLine(" " + EmpName);

}

}

//Client

classProgram

{

staticvoid Main(string[] args)

{

Worker Mitesh
= newWorker() { EmpId = 1, EmpName = "Mitesh" };

Worker Jay = newWorker() { EmpId
= 2, EmpName = "Jay" };

Worker Sujata
= newWorker() { EmpId = 3, EmpName = "Sujata" };

Worker Bhavesh
= newWorker() { EmpId = 4, EmpName = "Bhavesh" };

Manager Jatin =
newManager() { EmpId = 5, EmpName = "Jatin" };

Manager Kalpesh
= newManager() { EmpId = 6, EmpName = "Kalpesh" };

Manager Nitin =
newManager() { 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.