Composite design sample is a tree construction containing particular person objects blended with compositions of objects. That means, objects that produce other objects as their youngsters.

This sample permits us to have a tree construction and ask every node within the tree construction to carry out a activity. It creates a tree construction of a bunch of objects. 

A composite object is an object which comprise different objects. Other objects could comprise different composite objects. The object which doesn’t comprise another object is easy handled 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 value we have to present the mouse value solely. But for mom board value we have to present the CPU & RAM value.

The elementary concept is when you carry out some operation on the leaf object then the identical operation must be carried out on the composite objects. For instance, if we capable of get the value of a mouse, then we should always be capable to get the value of mom board and even we should always be capable to get the value of the pc object. If we need to implement one thing like this, then we have to use the composite design sample.

public interface IComponent
 {
        void ShowPrice();
 }
public class Leaf : IComponent
 {
        public int Price { get; set; }
        public string Name { get; set; }
        public Leaf(string title, int value)
        {
            this.Price = value;
            this.Name = title;
        }
        
        public void ShowPrice()
        {
            Console.WriteLine(Name +" : "+ Price);
        }
 }
public class Composite : IComponent
 {
        public string Name { get; set; }
        List<IComponent> elements = new List<IComponent>();
        public Composite(string title)
        {
            this.Name = title;
        }
        public void AddComponent(IComponent element)
        {
            elements.Add(element);
        }
        
        public void ShowPrice()
        {
            Console.WriteLine(Name);
            foreach (var merchandise in elements)
            {
                merchandise.ShowPrice();
            }
        }
 }
public class Program
 {
        static void Main(string[] args)
        {
            //Creating Leaf Objects
            IComponent exhaustingDisk = 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 cupboard = new Composite("Cabinet");
            Composite peripherals = new Composite("Peripherals");
            Composite pc = new Composite("Computer");
            
            //Creating tree construction
            //Ading CPU and RAM in Mother board
            motherBoard.AddComponent(cpu);
            motherBoard.AddComponent(ram);
            
            //Ading mom board and exhausting disk in Cabinet
            cupboard.AddComponent(motherBoard);
            cupboard.AddComponent(exhaustingDisk);
            
            //Ading mouse in peripherals
            peripherals.AddComponent(mouse);           
            
            //Ading cupboard and peripherals in pc
            pc.AddComponent(cupboard);
            pc.AddComponent(peripherals);
            
            //To show the Price of Computer
            pc.ShowPrice();                       
            Console.WriteLine();
            
            //To show the Price of Mouse
            mouse.ShowPrice();
            Console.WriteLine();

            //To show the Price of Cabinet
            cupboard.ShowPrice();
            Console.Read();
        }
 }

In composite design sample there are three contributors.

Component
An summary class or interface containing the members that might be carried out by all of the youngster objects within the hierarchy. It additionally implements among the conduct that’s frequent to all objects within the composition. This summary class acts as the bottom class for all of the objects throughout the hierarchy. In our instance, it’s the IComponent interface.

Leafs
A category to characterize the leaf conduct within the composition. In our instance, it’s the Leaf class.

Composite
The Composite defines the conduct for the elements which have youngsters (i.e. Leaves). It additionally shops its youngster elements and implements Add, Remove, Find and Get strategies to do operations on youngster elements. In our instance, it’s the Composite class.

Source link