Explanation of Composite Design Pattern

Explanation of Composite Design Pattern

Hello everyone in this article we are going to talk about Composite Design Pattern. We will talk about what composite design pattern and we will make an example on that with C# language and Visual Studio.

Now Let's begin.

Firstly, What is Composite Design Pattern?

Composite design pattern is a Design pattern which creates the sub somponents and objects is hierarchical. All components which be used in tree structure and single instance creates all of components with this related tree structure.

For this design pattern, it is good to know some terms:

  • Client: The application or main instance.
  • Component: It is the class which derive the composite structure or component.
  • Leaf: Every single objects in the structure.
  • Composite: The structure which contains components and leafs.

When we should use this design pattern:

If we have multiple type components or objects which are responsible for the same operation and also can be in a hierarchical pattern, we can use this design pattern.

Now,
I want you to think you have a factory that produce aircrafts. In here All employees and all machines using the some parts to produce an aircraft. Some of them are working to produce some small aircrafts and some of them are working for big aircrafts, also some of them are working for both of them. In here:
  • Client: Is You. Because you want them to produce an aircaft.
  • Component: The machines and the employees which are working to produce an aircraft.
  • Leaf: All parts of aircrafts.
  • Composite: The structure that contains All of parts, employees and machines to produce an aircraft together.

We called machines and employees here because there are so many kinds of machines and employees in here.

Below image you can see the schematic of my composite design pattern example:

Composite Design Pattern Schematic

Now, let's make example:

Employees.cs


using System;

namespace CompositePattern_Example
{
    class Employees
    {
        //I will keep my employees in here
        private int employee_count = 0;
        private string[] employees = new string[10];
        private string[] emp_roles = new string[10];

        public void addEmploye(string name, string role)
        {
            employees[employee_count] = name;
            emp_roles[employee_count] = role;
            employee_count++;
        }

        public void listEmployees()
        {
            Console.WriteLine("There are " + employee_count + " employees ");
            for (int i = 0; i < employee_count; i++)
            {
                Console.WriteLine("Name: " + employees[i] + ". Role : " + emp_roles[i]);
            }
        }
    }
}

Machines.cs


using System;

namespace CompositePattern_Example
{
    class Machines
    {
        //I will keep my machines in here
        private int machine_count = 0;
        private string[] machiness = new string[10];
        private string[] mac_types = new string[10];

        public void addMachine(string name, string type)
        {
            machiness[machine_count] = name;
            mac_types[machine_count] = type;
            machine_count++;
        }

        public void listMachines()
        {
            Console.WriteLine("There are " + machine_count + " Machines ");
            for (int i = 0; i < machine_count; i++)
            {
                Console.WriteLine("Machine: " + machiness[i] + " and Type : " + mac_types[i]);
            }
        }
    }
}

Parts.cs


using System;

namespace CompositePattern_Example
{
    class Parts
    {
        //I will keep my parts in here
        private int part_count = 0;
        private string[] parts = new string[10];

        public void addPart(string name)
        {
            parts[part_count] = name;

            part_count ++;
        }

        public void listParts()
        {
            Console.WriteLine("There are " + part_count + " parts ");
            for (int i = 0; i < part_count; i++)
            {
                Console.WriteLine("Machine: " + parts[i] );
            }
        }
    }
}

Program.cs


using System;

namespace CompositePattern_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            //These are my complex and single classes
            Employees employees = new Employees();
            Machines machines = new Machines();
            Parts parts = new Parts();

            employees.addEmploye("Burak", "Engineer");
            employees.addEmploye("Hamdi", "Mechanic");
            employees.addEmploye("TUFAN", "Developer");
            employees.addEmploye("TheCodeProgram", "Boss");

            machines.addMachine("Driller", "Mechanical");
            machines.addMachine("Computer", "Electronic");

            parts.addPart("Wing");
            parts.addPart("WEngine");
            parts.addPart("Landing Gears");
            parts.addPart("Rudder");

            Console.WriteLine("##The aircraft was produced by :##");
            employees.listEmployees();
            Console.WriteLine(" ##using## ");
            machines.listMachines();
            Console.WriteLine(" ##with get combined##");
            parts.listParts();

            Console.ReadLine();
        }
    }
}

Below image you can see the output of the example:

Composite Design Pattern Output

That is all in this article.

Have a great compositing the components.

Burak hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...