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. |
Let's take a look at the UML of the composite pattern first, then we will see an example. 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 see an example. 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 does 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 does 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)
{
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);
}
}
Future series of articles on sharepoint:
- sharepoint list -- the concepts of sharepoint list and how to effectively manage it
sharepoint version control -- the internals of sharepoint version control and how to administer and manage the versions
sharepoint permissions -- how to manage the permissions in a large enterprise sharepoint environment
sharepoint server farm -- how to set up a high availability sharepoint server farm
sharepoint document library -- the details on how to get your ways around the document library in sharepoint
sharepoint configuration -- the configurations needed for different sharepoint network scenarios
sharepoint css -- making the most out of customizing sharepoint frontend
sharepoint web services -- some of the most convienent ways to communicate with the internals of sharepoint