BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Composite Design Pattern

The composite pattern is a design pattern that is used when creating hierarchical object models. The pattern defines a manner in which to design recursive tree structures of objects, where individual objects and groups can be accessed in the same manner.

What is the Composite Pattern?

The composite pattern is a Gang of Four design pattern. This is a structural pattern as it defines a manner for creating relationships between classes or entities. The composite design pattern is used to create hierarchical, recursive tree structures of related objects where any element of the structure may be accessed and utilised in a standard manner. This includes individual leaf objects and those at any branch of the tree.

An example use of the composite pattern is for representing management structures in an organisation. In such a structure, each employee object can be the manager or zero or more subordinates.

Implementing the Composite Pattern

Composite Design Pattern UML

The UML class diagram above describes an implementation of the composite design pattern. The items in the diagram are described below:

  • Component. This abstract class is the base class for all objects that can appear within the hierarchical tree structure. The base class defines any standard members that will be implemented by all objects in the hierarchy. If you do not wish to create any actual functionality in this class you may decide to use an interface instead.
  • Composite. The Composite class is the key element of the design pattern. This class includes methods to add and remove child components and to retrieve those children from a private array or collection. Each of those components could also be a composite containing its own children. In the diagram the Composite class implements the generic IEnumerable interface. This interface is not actually a part of the basic composite design pattern. It is included here as often you will want to allow the use of a foreach loop to iterate through an object's children.
  • Leaf. This class is used to define components within the tree that cannot have children.

The following shows the basic code of the component design pattern implemented using C#. An iterator has been included to allow the use of the foreach command to traverse the children of any composite node in the tree. This uses the C# iterator syntax provided in C# 2.0. If you wish to use an earlier version of the language you must replace this with full implementations of the IEnumerable interfaces.

NB: The sample code uses the System.Collections namespace so ensure that you have included the using System.Collections; directive in the code.

public abstract class Component
{
    public abstract void Operation();
}


public class Composite : Component, IEnumerable<Component>
{
    private List<Component> _children = new List<Component>();

    public void AddChild(Component child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Component child)
    {
        _children.Remove(child);
    }

    public Component GetChild(int index)
    {
        return _children[index];
    }

    public override void Operation()
    {
        string message = string.Format("Composite with {0} child(ren).", _children.Count);
        Console.WriteLine(message);
    }

    public IEnumerator<Component> GetEnumerator()
    {
        foreach (Component child in _children)
        yield return child;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}


public class Leaf : Component
{
    public override void Operation()
    {
        Console.WriteLine("Leaf.");
    }
}
21 December 2008