DCSIMG
Composite Pattern - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Composite Pattern

Composite Pattern

Today it's the turn of composite pattern to be revealed.
You can read my previous posts on structural patterns here:
Decorator pattern 
Proxy pattern
Facade pattern
Adapter pattern

What is Composite Pattern?
The composite pattern is enabling us to treat single component or a composition (group) of
components in the same way. There are typical operations that can be performed on these
components which include operations like add, remove, get item and so on.
A simple example is the activity model of windows workflow foundation.
Activities are the building blocks of workflow. Every activity is easy to create, either
from writing code or by composing it from other activities. Therefore you can use activities
on their own or you can make activities that include activities inside.
The activities inherit from CompositeActivity which gives them the composite abilities.
For a UML diagram of the pattern go to dofactory site.

C# Example
Lets  look at an example of composite pattern.

    #region Interface For The Composite Pattern

 

    public interface IComponent

    {

        #region Properties

 

        string Name

        {

            get;

            set;

        }

 

        #endregion

 

        #region Methods

 

        void Add(IComponent component);

        void Remove(IComponent component);

        void Display(int depth);

 

        #endregion

    }

 

    #endregion

 

    #region Compoite Leaf

 

    public class Component : IComponent

    {

        #region Ctor

 

        /// <summary>

        /// Consruct a new Component object with the

        /// given name

        /// </summary>

        /// <param name="name">The given names</param>

        public Component(string name)

        {

            Name = name;

        }

 

        #endregion

 

        #region IComponent Members

 

        /// <summary>

        /// The name of the component

        /// </summary>

        public string Name

        {

            get; set;

        }

 

        /// <summary>

        /// This method isn't implemented because a component

        /// is a leaf of the composite

        /// </summary>

        public void Add(IComponent component)

        {

            throw new NotImplementedException("Can't add component");

        }

 

        /// <summary>

        /// This method isn't implemented because a component

        /// is a leaf of the composite

        /// </summary>

        public void Remove(IComponent component)

        {

            throw new NotImplementedException("Can't remove component");

        }

 

        /// <summary>

        /// Displays the depth of the component

        /// </summary>

        /// <param name="depth">The depth of the component</param>

        public void Display(int depth)

        {

            Console.WriteLine(

                string.Format("The {0} is in depth {1}", Name, depth));

        }

 

        #endregion

    }

 

    #endregion

 

    #region Composite

 

    public class Composite : IComponent

    {

        #region Members

 

        private List<IComponent> children;

 

        #endregion

 

        #region Ctor

 

        /// <summary>

        /// Construct a new Composite object with the give name

        /// </summary>

        /// <param name="name">The given name</param>

        public Composite(string name)

        {

            Name = name;

            children = new List<IComponent>();

        }

 

        #endregion

 

        #region IComponent Members

 

        /// <summary>

        /// The name of the composite

        /// </summary>

        public string Name

        {

            get; set;

        }

 

        /// <summary>

        /// Adds the given component to the composite

        /// </summary>

        /// <param name="component">The component to add</param>

        public void Add(IComponent component)

        {

            children.Add(component);

        }

 

        /// <summary>

        /// Removes the given component form the composite

        /// </summary>

        /// <param name="component">The component to remove</param>

        public void Remove(IComponent component)

        {

            children.Remove(component);

        }

 

        /// <summary>

        /// Display the depth of the composite and it's

        /// children

        /// </summary>

        /// <param name="depth">The given depth to display</param>

        public void Display(int depth)

        {

            Console.WriteLine(

                string.Format("The {0} is in depth {1}", Name, depth));

 

            foreach (IComponent component in children)

            {

                component.Display(depth + 1);

            }

        }

 

        #endregion

    }

 

    #endregion

We have 3 players in the example - the IComponent, the Component and the Composite.
The IComponent is the API of the Composite and the Component.
The Component is the leaf of the composed tree of Components.
The Composite is the object to compose IComponent objects.
You can build a hierarchal tree of components using the Composite object.

Summary
Lets sum up the post. The composite pattern is widely used.
You should use the composite pattern whenever you encounter an hierarchical structure
of objects and composite of these objects.
In the next post in this series I'm going to write about the bridge pattern.

Comments

Gil Fink Blog said:

The first pattern in the creational pattern series is the singleton pattern. You can read my previous

# April 23, 2008 9:23 AM

Gil Fink's Blog said:

In this post I give an introduction to the third design patterns type - the behavioral patterns.

# June 13, 2008 6:18 PM

Gil Fink's Blog said:

In this post I give an introduction to the creational patterns.

# June 13, 2008 6:33 PM

Gil Fink's Blog said:

In the post I introduce the flyweight design pattern and show a C# example.

# June 13, 2008 6:34 PM

Gil Fink's Blog said:

In the post I describe the bridge design pattern and show an example in C#.

# June 13, 2008 6:36 PM

Gil Fink's Blog said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 1:07 PM