Read-On to Know About the Latest Technological Developments

Blog

Composite Design Pattern

Way to Implement Composite Design Pattern in C# by Website Designing Company

Design patterns are tried, tested and proven solutions to repeated problems encountered by designers in software design. When used discreetly, the patterns can aid in solving complicated design problems quite effortlessly. The existence of the Gang of Four (GOF) is quite well-known in the designing industry and is divided into Creational, Structural or Behavioral. The Composite design pattern comes under the Structural category which aids in implementing a composite structure of objects in a manner that the clients can treat them and the compositions in a uniform manner as well. The website designing company has started taking advantage of this Composite pattern.

The Crux of Composite Design Pattern

The Composite design pattern enables you to compose an entire object multiple objects to represent a whole or part-whole hierarchy. For instance, a composite structure can be built like a tree using multiple objects which can be the leaves forming the tree. To be precise, the particular pattern enables you to single out an abstraction from the actual execution.

Parts of Composite Pattern

The Composite pattern constitutes certain parts and they are:

Components- This signifies either an interface or an abstract class which serves as the base type for all objects in the hierarchy.

Composite Element- This is signified by using a concrete class defining essential operations that can be performed on the child components.

Leaf- This denotes a leaf component and is described using a class and it does not contain any subclass.

If we consider the following class, named Employee. This is an abstract class representing the “Component” in the design. It constitutes abstract methods to add an employee, remove him/her and also receive data pertaining to one of 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 class Employee contains an argument constructor to attribute values to the name and salary variables.

The next class in the design is called “TeamLead” and it denotes the “Composite” type in the design. It extends the Employee class and renders the definition for the abstract methods.

{

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();

foreach (Employee emp in lstEmployee)

{

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

}

return sbEmployee.ToString();

}

}

The final class in the design is the TeamMember class that denotes the lead node in the composite structure. here’s how this class looks:

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 does not have any offshoot. Note that since the teamMember class denotes a leaf node in the composite structure, nothing is implemented of the “Add” or the “Remove” methods in this class.

The following code snippet elaborates on how one can create instances of the Employee class; add them to the employee’s collection and finally exhibit 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 the designer executes the above code snippet, the details of the employees who are part of a particular team will be exhibited in the console window.



Comments (0)

Leave a comment






The answer is