The Composite Design pattern is structural design pattern. This design pattern is used when we want to treat a group of objects in same way. The composite design pattern has following elements.

Component

This is a class that contain all the members that needs to be implemented by all the objects.

Composite

This class is used to add, remove and traverse components.

Leaf

The leaf object in tree structure is defined as leaf element.

Code Sample

I want to print salaries of all employees in my organization with names and designation. The root of organization tree is CEO. The CEO has directors as child composite. The director has managers as child composite. Finally, engineers at level 1 as leaf because no one is reporting them.

        // ignore this, just to print 
        static int i = 0;

        interface IComponent
        {
            void print();
        }


        class Composite : IComponent
        {
            private string name;
            private string designation;
            private decimal salary;
            private List<IComponent> listComponent = new List<IComponent>();

            public Composite(String name, String designation, decimal salary)
            {
                this.name = name;
                this.designation = designation;
                this.salary = salary;
            }

            public void addComponent(IComponent component)
            {
                listComponent.Add(component);
            }

            public void printDesignation()
            {
                Console.Write(designation);
                Console.Write(" ");
            }

            public void printName()
            {
                Console.Write(name);
                Console.Write(" ");

            }

            public void printSalary()
            {
                Console.Write(salary);
                Console.Write(" ");

            }

            public void print()
            {
                printName();
                printDesignation();
                printSalary();
                Console.WriteLine(" ");
                i++;

                foreach (var component in listComponent)
                {
                    for(int k =0;k <i; k++)
                    {
                        Console.Write(" - ");
                    }
                    component.print();

                }
            }
        }

        class Leaf : IComponent
        {
            private string name;
            private string designation;
            private decimal salary;

            public Leaf(String name, String designation, decimal salary)
            {
                this.name = name;
                this.designation = designation;
                this.salary = salary;
            }

            public void print()
            {
                printName();
                printDesignation();
                printSalary();
                Console.WriteLine(" ");
            }

            public void printDesignation()
            {
                Console.Write(designation);
                Console.Write(" ");

            }

            public void printName()
            {
                Console.Write(name);
                Console.Write(" ");

            }

            public void printSalary()
            {
                Console.Write(salary);
                Console.Write(" ");

            }
        }




        static void Main(string[] args)
        {
            Leaf ram = new Leaf("Ram", "SE1", 1000000);
            Leaf sham = new Leaf("Sham", "SE1", 1000000);

            Composite teamLead = new Composite("Mohan", "Team Lead", 1000000);
            teamLead.addComponent(ram);
            teamLead.addComponent(sham);

            Composite manager = new Composite("John", "Manager", 1000000);
            manager.addComponent(teamLead);

            Composite director = new Composite("kuku", "Director", 1000000);
            director.addComponent(manager);

            //traverse through all
            director.print();
        }
    }

Output:

    kuku Director 1000000
    - John Manager 1000000
    -  - Mohan Team Lead 1000000
    -  -  - Ram SE1 1000000
    -  -  - Sham SE1 1000000