Composite design model implementation in C #

0

Design patterns are tested and proven solutions to recurring problems that are often encountered in software designs. When used wisely, they can help you solve complex design problems with ease.

GOF (Gang of Four) design patterns are categorized as creative, structural, or behavioral. The Composite design model belongs to the Structural category and helps you implement a composite structure of objects so that customers can process objects as well as compositions in a uniform manner.

What is the Composite design model?

The Composite design model allows you to compose an entire object from one or more individual objects to represent a partial-entire hierarchy. As an example, we can build a tree (a composite structure) using multiple objects (also called the leaves of the tree). In essence, the Composite design pattern allows you to isolate an abstraction from the actual implementation.

The participants

The participants in the composite design model are:

  • Component – This is represented by an abstract class or interface and serves as the base type for all objects in the hierarchy
  • CompositeElement – This is represented with the help of a concrete class that defines the necessary operations that can be performed on child components i.e. methods Add, Remove, Get, Find, etc.
  • Leaf – This represents a leaf component and is defined using a class that has no subclass

Consider the next class, named Employee. This is an abstract class and represents the “component” in this design. It contains abstract methods to add an employee, delete an employee and also get data relating to one or more employees.

 public abstract class Employee

    {

        protected string name;

        protected double salary;

        public Employee(string name, double salary)

        {

            this.name = name;

            this.salary = salary;

        }

        public abstract void Add(Employee employee);

        public abstract void Remove(Employee employee);

        public abstract string GetData();

    }

The abstract Employee class also contains an argument constructor for assigning values ​​to the name and salary variables.

The next class in our design is called “TeamLead”, and it represents the “Composite” type in our design, extends the Employee class, and provides the definition of abstract methods.

public class Leader : Employee

    {

        List lstEmployee = new List();

        public TeamLead(string name, double salary) : base(name, salary) { }

        public override void Add(Employee employee)

        {

            lstEmployee.Add(employee);

        }

        public override void Remove(Employee employee)

        {

            lstEmployee.Remove(employee);

        }

        public override string GetData()

        {

            StringBuilder sbEmployee = new StringBuilder();

             for each (Employee emp in lstEmployee)

            {

                sbEmployee.Append(emp.GetData()+ "n");

            } 

            return sbEmployee.ToString();

        }

    }

The last class in our design is the TeamMember class which represents the leaf node in the composite structure. Here’s what this class looks like:

public class TeamMember : Employee

    {

        public TeamMember(string name, double salary) : base(name, salary) { }

        public override void Add(Employee employee)

        {

            //Operation not permitted since this is a leaf node.

        }

        public override void Remove(Employee employee)

        {

            //Operation not permitted since this is a leaf node.

        }

        public override string GetData()

        {

            return "Name: "+ name + "tSalary: "+salary.ToString("N2");

        }

    }

A leaf node in a composite structure has no children, does it? Note that since the TeamMember class represents a leaf node in the composite structure, there are no implementations of the “Add” or “Remove” methods in this class.

The following code snippet demonstrates how you can create instances of the Employee class, add them to the employee’s collection, and then display the data in the console window.

static void Main(string[] args)

    {

      Employee employeeA = new TeamMember("Joydip Kanjilal", 20000);

      Employee employeeB = new TeamMember("Samuel Jones", 45000);

      Employee teamLead = new TeamLead("Steve Smith", 75000);

      teamLead.Add(employeeA);

      teamLead.Add(employeeB);

      Console.WriteLine(teamLead.GetData());

      Console.Read();

    }

When you run the code snippet above, the details of the two employees on Steve’s team are displayed in the console window. You can read more about the composite design pattern here.

Copyright © 2017 IDG Communications, Inc.


Source link

Share.

Leave A Reply