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:

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 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 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 do 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:

Hello,
You are doing a nice work with this pattern stuff, and I will suggest you also try to
look for possible/practical usage of these patterns in the .NET framework itself and
other libraries, as illustrations to juice it up