November 23, 2020
composite-pattern

Composite Pattern

Composite design pattern is a tree structure containing individual objects mixed with compositions of objects. That means, objects that have other objects as their children.

This pattern allows us to have a tree structure and ask each node in the tree structure to perform a task. It creates a tree structure of a group of objects. 

A composite object is an object which contain other objects. Other objects may contain other composite objects. The object which does not contain any other object is simple treated as leaf of object. 

Consider the above diagram. Here Computer, Cabinet, Peripherals, Mother Boards are composite object. HDD, CUP, RAM, mouse’s are leaf object.

For mouse price we need to show the mouse price only. But for mother board price we need to show the CPU & RAM price.

The fundamental idea is if you perform some operation on the leaf object then the same operation should be performed on the composite objects. For example, if we able to get the price of a mouse, then we should be able to get the price of mother board and even we should be able to get the price of the computer object. If we want to implement something like this, then we need to use the composite design pattern.

public interface IComponent
 {
        void DisplayPrice();
 }
public class Leaf : IComponent
 {
        public int Price { get; set; }
        public string Name { get; set; }
        public Leaf(string name, int price)
        {
            this.Price = price;
            this.Name = name;
        }
        
        public void DisplayPrice()
        {
            Console.WriteLine(Name +" : "+ Price);
        }
 }
public class Composite : IComponent
 {
        public string Name { get; set; }
        List<IComponent> components = new List<IComponent>();
        public Composite(string name)
        {
            this.Name = name;
        }
        public void AddComponent(IComponent component)
        {
            components.Add(component);
        }
        
        public void DisplayPrice()
        {
            Console.WriteLine(Name);
            foreach (var item in components)
            {
                item.DisplayPrice();
            }
        }
 }
public class Program
 {
        static void Main(string[] args)
        {
            //Creating Leaf Objects
            IComponent hardDisk = new Leaf("Hard Disk", 2000);
            IComponent ram = new Leaf("RAM", 3000);
            IComponent cpu = new Leaf("CPU", 2000);
            IComponent mouse = new Leaf("Mouse", 2000);            
            
            //Creating composite objects
            Composite motherBoard = new Composite("Peripherals");
            Composite cabinet = new Composite("Cabinet");
            Composite peripherals = new Composite("Peripherals");
            Composite computer = new Composite("Computer");
            
            //Creating tree structure
            //Ading CPU and RAM in Mother board
            motherBoard.AddComponent(cpu);
            motherBoard.AddComponent(ram);
            
            //Ading mother board and hard disk in Cabinet
            cabinet.AddComponent(motherBoard);
            cabinet.AddComponent(hardDisk);
            
            //Ading mouse in peripherals
            peripherals.AddComponent(mouse);           
            
            //Ading cabinet and peripherals in computer
            computer.AddComponent(cabinet);
            computer.AddComponent(peripherals);
            
            //To display the Price of Computer
            computer.DisplayPrice();                       
            Console.WriteLine();
            
            //To display the Price of Mouse
            mouse.DisplayPrice();
            Console.WriteLine();

            //To display the Price of Cabinet
            cabinet.DisplayPrice();
            Console.Read();
        }
 }

In composite design pattern there are three participants.

Component
An abstract class or interface containing the members that will be implemented by all the child objects in the hierarchy. It also implements some of the behavior that is common to all objects in the composition. This abstract class acts as the base class for all the objects within the hierarchy. In our example, it is the IComponent interface.

Leafs
A class to represent the leaf behavior in the composition. In our example, it is the Leaf class.

Composite
The Composite defines the behavior for the components which have children (i.e. Leaves). It also stores its child components and implements Add, Remove, Find and Get methods to do operations on child components. In our example, it is the Composite class.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

One thought on “Composite Pattern

Leave a Reply

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