Composite pattern is a collection of objects where one of the objects in the collection can branch out. When using this pattern it usually consist of the following elements a component, leaf and composite.
Component:
- is an abstraction of all components, including the composite ones.
- declares the interface for the objects in the composition.
Leaf:
- represents a leaf object in the composition.
- implements all the Component’s methods.
Composite:
- represents a composite component.
- implements methods for manipulating children.
- implements all Component’s methods
Below are some code snippet taken from the design-patterns repository from my github account, the console application is called Composite and uses a Parent (Composite) and Child (Leaf) objects to represent the use of the pattern.
IMember Interface:
The IMember interface represents the Component and it consist of a method signature called Print(). This interface will be implemented by the Parent and Child class and they will also implement the Print() method for outputing information, but I will explain this later.
using System; namespace composite { public interface IMember { void Print(); } }
Child Class:
The Child class represent the Leaf component which implements the IMember interface and it’s methods. This class has a constructor that takes in string name as a parameter which will be outputted to the console by the Print() method when it is invoked.
using System; namespace composite { /// /// This is a class that represents a child from the composite pattern. /// public class Child:IMember { private string _name; public Child(string name) { _name = name; } public void Print() { Console.WriteLine("Child: {0}", _name); } } }
Parent Class:
The Parent class represents the composite component, and it implements the IMember interface and it’s methods. This class looks similar to the Child class except this class contain methods for adding and removing child components.
In this class I have added a List of IMember for storing a collection of Child objects. The AddChildren and RemoveChildren method allows me to add and remove a child from the collection. The Print() method ouputs the parent’s name and all the child’s name from the collection.
namespace composite { public class Parent:IMember { private string _name; private List members = new List(); public Parent(string name) { _name = name; } public void Print() { Console.WriteLine("Parent: {0}", _name); foreach (var member in members) { member.Print(); } } public void AddChildren(IMember member) { members.Add(member); } public void RemoveChildren(IMember member) { members.Remove(member); } } }
Below is the main class the uses all the components demonstrated above. I have created a parent1 object and gave it a name called David and he has two chidren called Rosey and Greg. Parent2 called Susan also has three children called Jack, Jill and David, but David has his own children too. So what I have written here is a simple demonstration of a composite pattern for creating a family tree.
using System; namespace composite { /// /// Composite Pattern demonstration. /// class MainClass { static void Main(string[] args) { // Parent 1 has two children Rosey and Greg. var parent1 = new Parent("David"); parent1.AddChildren(new Child("Rosey")); parent1.AddChildren(new Child("Greg")); // Parent 2 has two children Jack and Jill. var parent2 = new Parent("Susan"); parent2.AddChildren(new Child("Jack")); parent2.AddChildren(new Child("Jill")); // Parent 1 is added to parent 2 because parent 1 // is a child who is a parent now. parent2.AddChildren(parent1); // Output the parent and child hierachy. parent2.Print(); } } }
Source Code:
- GitHub Repository – Design Patterns. Full source code for this post can be pulled from my GitHub repository.
Reference: