Composite Pattern in java
The composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in
the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole
hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.
When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code
more complex, and therefore, error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In
object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar
functionality. This is known as a "has-a" relationship between objects. The key concept is that you can manipulate a single instance of the
object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a least common
denominator relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a
group of shapes to have the same effect (in some sense) as resizing a single shape.
Composite should be used when clients should ignore the difference between compositions of objects and individual objects.
If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them,
then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous. Below is the UML structure
of composite pattern.

Class representing Component:
package com.java2novice.dp.composite.pattern;
public interface Employee {
public void showEmployeeDetails();
}
|
Class representing Leaf
package com.java2novice.dp.composite.pattern;
public class Engineer implements Employee {
private String name;
private long empId;
private String department;
public Engineer(long empId, String name, String department){
this.empId = empId;
this.name = name;
this.department = department;
}
@Override
public void showEmployeeDetails() {
System.out.println(empId+" ***** "+name+" ***** "+department);
}
}
|
Class representing Leaf
package com.java2novice.dp.composite.pattern;
public class Accountant implements Employee {
private String name;
private long empId;
private String department;
public Accountant(long empId, String name, String department){
this.empId = empId;
this.name = name;
this.department = department;
}
@Override
public void showEmployeeDetails() {
System.out.println(empId+" ***** "+name+" ***** "+department);
}
}
|
Class representing Composite
package com.java2novice.dp.composite.pattern;
import java.util.ArrayList;
import java.util.List;
public class CompanyDirectory implements Employee {
private List<Employee> employeeList = new ArrayList<Employee>();
@Override
public void showEmployeeDetails() {
for(Employee emp:employeeList){
emp.showEmployeeDetails();
}
}
public void addEmployee(Employee emp){
employeeList.add(emp);
}
public void removeEmployee(Employee emp){
employeeList.remove(emp);
}
}
|
Class representing Client
package com.java2novice.dp.composite.pattern;
public class Company {
public static void main(String a[]){
Engineer eng1 = new Engineer(100, "Nataraj", "Engineering");
Engineer eng2 = new Engineer(101, "Ravi", "Engineering");
CompanyDirectory engDirectory = new CompanyDirectory();
engDirectory.addEmployee(eng1);
engDirectory.addEmployee(eng2);
Accountant acc1 = new Accountant(200, "Gopi", "Accounts");
Accountant acc2 = new Accountant(201, "RamGopal", "Accounts");
CompanyDirectory accDirectory = new CompanyDirectory();
accDirectory.addEmployee(acc1);
accDirectory.addEmployee(acc2);
CompanyDirectory directory = new CompanyDirectory();
directory.addEmployee(engDirectory);
directory.addEmployee(accDirectory);
directory.showEmployeeDetails();
}
}
|
|