Header Code

Composite Design Pattern 14Dec, 2012

Here, I’m going to share with you my understanding of the Composite Pattern. Composite Pattern is used to work with hierarchical structures where both the primitive and composite objects are treated alike. The easiest example to understand could be a Tree or Navigation Menu which we generally see in Windows Explorer on the left side. It looks something like this


Here, all the folders can be treated as composite objects which can themselves contain more folders and files. Files are the Primitive objects or leaves in the tree.

Both the primitive and the composite object will have an operation. The composite object’s operation will call the operations on all the child objects.

Lets have a look at the UML of this


Here, Component can be an interface or a class, which is inherited by both the Composite and the Leaf object. Composite Object will have the methods to add and remove the Leaf or other Composite Objects to a collection, which acts as the Child objects of that Composite.

Lets Have a look at the Sample Code. Consider a tree like navigation menu (similar to one shown in Figure 1), generally used as Sitemap (www.zapbuild.com/sitemap). But we only want to show those links to user which he is authorized to view. And if all the links in a category are not visible then the category itself will not be visible.


We can write a generalized solution as follows:

Class Component
private string DisplayText;

public virtual bool IsAuthorized()
//Logic to check the User Authorization

Class Composite : Component

Public List<Component> Items;

Public void AddComponent(Component Item)

public override bool IsAuthorized()
bool authorized = base.IsAuthorized();

bool childItemAuthorized = false;

if (this.Items.Count > 0)
//If any one of the items in this Composite Object
//is Authorized
foreach (Component item in this. Items)
if (item.IsAuthorized() == true)
childItemAuthorized = true;
childItemAuthorized = true;

//If the object itself and any one of the Child Items in the composition
//are authorized, return true
this.isAuthorized = authorized && childItemAuthorized;

return this.isAuthorized;

public class Leaf : Component

Firstly, I have declared the “Component” as a Class and added the logic of IsAuthorized in it. I could have taken the Component as an interface and then will have to write the logic for the Leaf object. But this also depends upon individual problem you are working on.

In the Composite class, we have a collection of Component objects. This is because; a category can contain not only the Links but Sub Categories also. So, the collection can contain both the Composite and the Leaf objects.

The IsAuthorized method in the Composite class first authorizes itself and then each item in its collection. If none of the Child Items are visible then the parent is not visible either.

I hope all this makes sense.

This is a very simple depiction of the composite pattern and this code is written just for the sake of illustration.

Posted by: Amritpal Singh / In: Design Patterns and Tagged , ,