Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Composite Pattern

Posted by Paul on January 26, 2012

This is my last article on the 23 Design Patterns series.  I would like to thank all readers for their support and encouragements that made learn all the design patterns.

In this article we can explore the Composite Design Pattern. It provides a better approach in addressing communication with individual and group of objects. As usual we can start with the Challenge – Solution approach.


You are working on a distributed application containing Head Office and Branch Offices. Each office will be having 2 Clock Controls which needed to be updated globally during Daylight Saving times.


The above diagram shows the current structure. There will be only 1 Head Office and multiple branches under it. The head office and each branch will be having 2 clock controls. The time changing process starts from the head office and in the current approach, the head office has to remember all branches and all clocks inside each branch. This is very tedious and will break when there is consolidation of branches under one branch group.

How to provide a better solution?


Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly


We can use the Composite Design Pattern in the above scenario. Once our solution is implemented the Head Office needs to think about only the Branches. The Branches will think about their respective Clocks.

There are 2 classes in our solution: Office and Clock. The Office class can represent Head Office and Branches. The Clock class manages the clock control for changing time.

We need to introduce one interface which will be implemented by the Office and Clock classes.

interface IComponent


void Add (IComponent notifier);

void SetTime(DateTime time);


The Add() method ensures adding a component of type IComponent. It can be used to add an Office or Clock instance.

The SetTime() method can be used to change the current time.

Class Implementations

Following are the implementations of the Office and Clock classes:

class Office : IComponent


private IList<IComponent> _list = new List<IComponent>();

public void Add(IComponent notifier)




public void SetTime(DateTime time)


foreach (IComponent n in _list)




class Clock : IComponent


public ClockControl ClockControl;

public void Add(IComponent notifier)


throw new ApplicationException(“You cannot add IClock!”);


public void SetTime(DateTime time)


ClockControl.CurrentTime = time;



Please note that the SetTime() implementation of Office class takes care of informing all the added IComponent instances. Thus the Head Office can notify all the branches. The branches will notify all the clock instances.

Creating Instances

Following is the code of instance creation for Head Office and Branches.

_headOffice = new Office();

_headOffice.Add(new Clock() { ClockControl = hForm.clock1 });

_headOffice.Add(new Clock() { ClockControl = hForm.clock2 });

Office branch1 = new Office();

branch1.Add(new Clock() { ClockControl = b1Form.clock1 });

branch1.Add(new Clock() { ClockControl = b1Form.clock2 });


Office branch2 = new Office();

branch2.Add(new Clock() { ClockControl = b2Form.clock1 });

branch2.Add(new Clock() { ClockControl = b2Form.clock2 });


Please note that the branch instances are added to the head office.

Running the Application

On clicking the Set Time button you can see all the clocks are reset to 10:00 AM.


Here the Head Office does not need to think about all the clock instances of branches. Here the individual objects (clocks) and composite objects (branches) are treated uniformly satisfying the pattern definition.


In this article we have explored the Composite Pattern. It provides a centralized approach in talking with individual and combined objects. The attachment contains the example application including the clock control we have discussed.


One Response to “Composite Pattern”

  1. Here is another example of Composite Design pattern using c#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s