Composite Design Pattern
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
IComponentinterface defines the methods that both theCompositeclass and theLeafclass must implement. TheOperationmethod is the common method that all elements in the tree structure can perform. TheIComponentsimply represents an element in the tree. - The
Leafclass are elements that cannot have any elements below it, and it only hasOperationmethod to perform the task for the element. - The
Compositeclass are elements that can have 0 or more elements below it. The methods that it supports are as follows: - The
AddComponentmethod adds an element below it - The
GetChildmethod gets all the elements below it - The
Operationmethod performs the task for the element itself - The
RemoveComponentmethod 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
IEmployeeinterface defines the operation that all employees must be able to perform, which is theShowHappinessmethod. - The
Workerclass are the employees that do not manage anyone, and implements only theShowHappinessmethod. - The
Supervisorclass are the employees that can manage other employees and have the following variables and methods: - The
privatevariablesubordinateare the list of employees that the supervisor manages. - The
AddSubordinatemethod adds an employee under the supervisor. - The
ShowHappinessmethod 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.
发表评论
799Cb8 I cannot thank you enough for the blog article.Really thank you! Much obliged.


