Composite Pattern

Individual objects as well as the composite objects can be represented with the Composite Design Pattern.

Composite Pattern

Composite Pattern

     

Individual objects as well as the composite objects can be represented with the Composite Design Pattern. Composite pattern represents these objects with a tree structure. Suppose, Within a company, there is an employee hierarchy where a manager has its subordinates, Project Leader also have the subordinates, while the developer has no subordinates. 

Benefits : It is more flexible as compared to the static inheritance. It simplifies coding by implementing each feature in a class. It enhances the capability of an object as the new classes are created to add new features and make some changes. 

Usage: This design pattern is used when the responsibilities are needed to be added dynamically to the individual objects without affecting other objects. Where an object's responsibilities may vary from time to time. 

Now, lets try to solve the above problem technically. 

Develop a class "Employee" having the getters and setters for the attributes empname, empsal and emp subordinates.

Employee.java

class Employee {
String Empname;
double Empsalary;
Employee(String n, double s){
Empname = n;
Empsalary = s;
}
String getName() {
return Empname;
}
double getSalary() {
return Empsalary;
}
public String toString() {
return "Employee" + Empname;
}
}

For example, General Manager may have several employee and some of them are Managers, further these managers have several employees. To illustrate all these things, lets design a simple Manager class. 

Manager.java:

class Manager {
Manager manager;
Employee[] emply;
String dept;
Manager(Manager mgr,Employee[] e, String d ) {
this(e, d);
this.manager = mgr;
}

Manager(Employee[] e, String d) {
emply = e;
dept =d;
}
String getDept() {
return dept;
}
Manager getManager() {
return manager;
}
Employee[] getEmployee() {
return emply;
}
public String toString() {
return dept + " manager";
}
}

Here is the "Test" class that shows the information about the "Manager" class.

Test.java :

class Test {
public static void main(String[] args) {
Employee[] e1 = {new Employee("Zulfiqar"90),
new Employee("Amit"80)};
Manager m1 = new Manager(e1, "Accounting");

Employee[] e2 = {new Employee("Aquil"70),
new Employee("Ravi"60),
new Employee("Vinod"40)};

Manager m2 = new Manager(m1, e2, "Production");

System.out.println(m2);
Employee[] emp = m2.getEmployee();
if (emp != null)
for (int k = 0; k < emp.length; k++)
System.out.println(" "+emp[k]+" Salary: $"
emp[k].getSalary()); 

Manager m = m2.getManager();
System.out.println(" " + m);
if (m!= null) {
Employee[] emps = m.getEmployee();
if (emps != null)
for (int k = 0; k < emps.length; k++)
System.out.println(" " + emps[k]+" Salary: $"
emps[k].getSalary());


}
}
}

Here is the output of the program :

C:\> java Test
Production manager
Employee Zulfiqar Salary: $90.0
Employee Amit Salary: $90.0
Employee Aquil Salary: $80.0
Accounting manager
Employee Ravi Salary: $70.0
Employee Vinod Salary: $50.0

The above example concludes that the composite pattern allows us to create a tree like structure for both simple and complex objects.