Composite Design Pattern – Object Oriented Design – C Sharp

We will look at c sharp code of The Composite design pattern in object oriented design which arranges structured hierarchies so that single components and groups of components can be treated in the same way. Typical operations on the components include add, remove, display, find, and group.

composite-design-pattern-uml-diagram

composite-design-pattern-uml-diagram

Composite Pattern

Interface IComponent

Class Component Implements IComponent

Class Composite Implements IComponent

Class Client

Member Variables Member Variables Member Variables Member Variables
List<Component> components IComponent component
Member Functions Member Functions Member Functions Member Functions
Operation Operation Operation

 

Use it when
You have Irregular structure of objects and composites of objects
You want To treat all objects uniformly
Consider using
  1. Decorator for adding, removing and finding components
  2. Flyweight to share components
  3. Visitor to localize operation distributed in several components

C sharp code for Composite Design Pattern

class MainApp
    {
        static void Main()
        {
            var root = new ShapeTree { Node = new Shape("Picture") };
			root.Add(new Shape("Green Box"));
            var branch = root.Add(new Shape("Two Circles"));
            branch.Add(new Shape("Black Circle"));

            var shape = new Shape("Yellow Line");
            root.Add(shape);
            root.Remove(shape);
            root.Add(shape);
			ShapeTree.Display(root, 1);
            Console.ReadKey();
        }
    }
    class ShapeTree where T : IComparable
    {
        private List<ShapeTree> _children = new List<ShapeTree>();
		public ShapeTree Add(T child)
        {
            var newNode = new ShapeTree { Node = child };
            _children.Add(newNode);
            return newNode;
        }
	public void Remove(T child)
        {
            foreach (var treeNode in _children)
            {
                if (treeNode.Node.CompareTo(child) == 0)
                {
                    _children.Remove(treeNode);
                    return;
                }
            }
        }
	public T Node { get; set; }
	public List<ShapeTree> Children
        {
            get { return _children; }
        }
	public static void Display(ShapeTree node, int indentation)
        {
            string line = new String('-', indentation);
            Console.WriteLine(line + " " + node.Node);

            node.Children.ForEach(n => Display(n, indentation + 1));
        }
    }
    class Shape : IComparable
    {
        private string _name;
	public Shape(string name)
        {
            this._name = name;
        }
	public override string ToString()
        {
            return _name;
        }
	public int CompareTo(Shape other)
        {
            return (this == other) ? 0 : -1;
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *