A tree (family / recipe) structure of simple and composite objects. A composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality.
Sample Code Download: CompositeDesignPattern.zip (Visual Studio 2017 Console App)
Create an interface IEmployee
//Component
public interface IEmployee
{
void GetDetails(int indentation);
}
Create Employee Class and implement the interface component
public class Employee : IEmployee
{
public Employee(string name, string dept)
{
this.Name = name;
this.Department = dept;
}
public string Name { get; set; }
public string Department { get; set; }
public void GetDetails(int indentation)
{
Console.WriteLine(
$"{new String('-', indentation)}- Name:{Name.ToString()}, Dept:{Department} (Leaf) "
);
}
}
Create Manager Class which is the composite and implement the interface component and add the subordinates as shown below.
public class Manager : IEmployee
{
public List SubOrdinates;
public Manager(string name, string dept)
{
this.Name = name;
this.Department = dept;
SubOrdinates = new List();
}
public string Name { get; set; }
public string Department { get; set; }
public void GetDetails(int indentation)
{
Console.WriteLine();
Console.WriteLine($"{new String('-', indentation)}+ Name:{this.Name.ToString()}, " +
$"Dept:{this.Department} - Manager(Composite)");
foreach (var component in SubOrdinates)
{
component.GetDetails(indentation + 1);
}
}
}
Switch to the client which is main program and implement the below code
class Program
{
static void Main(string[] args)
{
IEmployee john = new Employee("John", "IT");
IEmployee mike = new Employee("Mike", "IT");
IEmployee jason = new Employee("Jason", "HR");
IEmployee eric = new Employee("Eric", "HR");
IEmployee henry = new Employee("Henry", "HR");
IEmployee james = new Manager("James", "IT") { SubOrdinates = { john, mike } };
IEmployee philip = new Manager("Philip", "HR") { SubOrdinates = { jason, eric, henry } };
//bob is managers manager
IEmployee bob = new Manager("Bob", "Head") { SubOrdinates = { james, philip } };
bob.GetDetails(1);
//print james and its leafs
james.GetDetails(1);
Console.ReadLine();
}
}
Sample Code Download: CompositeDesignPattern.zip (Visual Studio 2017 Console App)
Interface IEmpoyee
Create an interface IEmployee
//Component
public interface IEmployee
{
void GetDetails(int indentation);
}
Employee Class
Create Employee Class and implement the interface component
public class Employee : IEmployee
{
public Employee(string name, string dept)
{
this.Name = name;
this.Department = dept;
}
public string Name { get; set; }
public string Department { get; set; }
public void GetDetails(int indentation)
{
Console.WriteLine(
$"{new String('-', indentation)}- Name:{Name.ToString()}, Dept:{Department} (Leaf) "
);
}
}
Manager Class
Create Manager Class which is the composite and implement the interface component and add the subordinates as shown below.
public class Manager : IEmployee
{
public List
public Manager(string name, string dept)
{
this.Name = name;
this.Department = dept;
SubOrdinates = new List
}
public string Name { get; set; }
public string Department { get; set; }
public void GetDetails(int indentation)
{
Console.WriteLine();
Console.WriteLine($"{new String('-', indentation)}+ Name:{this.Name.ToString()}, " +
$"Dept:{this.Department} - Manager(Composite)");
foreach (var component in SubOrdinates)
{
component.GetDetails(indentation + 1);
}
}
}
Main
Switch to the client which is main program and implement the below code
class Program
{
static void Main(string[] args)
{
IEmployee john = new Employee("John", "IT");
IEmployee mike = new Employee("Mike", "IT");
IEmployee jason = new Employee("Jason", "HR");
IEmployee eric = new Employee("Eric", "HR");
IEmployee henry = new Employee("Henry", "HR");
IEmployee james = new Manager("James", "IT") { SubOrdinates = { john, mike } };
IEmployee philip = new Manager("Philip", "HR") { SubOrdinates = { jason, eric, henry } };
//bob is managers manager
IEmployee bob = new Manager("Bob", "Head") { SubOrdinates = { james, philip } };
bob.GetDetails(1);
//print james and its leafs
james.GetDetails(1);
Console.ReadLine();
}
}