Purpose of Composite Design Pattern

"Compose objects into tree structure to represent part-whole hierarchies. 

Composite lets client treat individual objects and compositions of objects uniformly".

Composite design pattern treats each node in two ways-CompositeĀ orĀ leaf. 
Composite means it can have other objects below it. Leaf does not have objects below it

Component
declares interface for objects in composition.
implements default behavior for the interface common to all classes as appropriate.
declares an interface for accessing and managing its child components.
Leaf
represents leaf objects in the composition. A leaf has no children.
defines behavior for primitive objects in the composition.
Composite
defines behavior for components having children.
stores child components.
implements child related operations in the component interface.
Client
manipulates objects in the composition through the component interface.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


interface Employee {

 public void add(Employee employee) throws Exception;
 public void remove(Employee employee);
 public Employee getChild(int i);
 public String getName();
 public double getSalary();
 public String getDesignation();
 public void print();
}


class Director implements Employee{

 private String name;
 private double salary;

 public Director(String name,double salary){
 this.name = name;
 this.salary = salary;
 }

 List<Employee> employees = new ArrayList<Employee>();
 public void add(Employee obj) throws Exception{
 String c_name = obj.getClass().getName();
 if(c_name.equalsIgnoreCase("Manager"))
 {
 employees.add(obj);
 }
 else
 {
 throw new Exception("non Manager not allowed to add");
 }
 
 }

 public Employee getChild(int i) {
 return employees.get(i);
 }

 public String getName() {
 return name;
 }

 public double getSalary() {
 return salary;
 }

 public String getDesignation(){
 return "Director";
 }
 public void print() {
 System.out.println("-------------");
 System.out.println("Name ="+getName());
 System.out.println("Salary ="+getSalary());
 System.out.println("-------------");

 Iterator<Employee> employeeIterator = employees.iterator();
 while(employeeIterator.hasNext()){
 Employee employee = employeeIterator.next();
 employee.print();
 }
 }

 public void remove(Employee employee) {
 employees.remove(employee);
 }
 }


class Manager implements Employee{

 private String name;
 private double salary;

 public Manager(String name,double salary){
 this.name = name;
 this.salary = salary;
 }

 List<Employee> employees = new ArrayList<Employee>();
 public void add(Employee employee) {
 employees.add(employee);
 }

 public Employee getChild(int i) {
 return employees.get(i);
 }

 public String getName() {
 return name;
 }

 public double getSalary() {
 return salary;
 }

 public String getDesignation(){
 return "Manager";
 }
 public void print() {
 System.out.println("-------------");
 System.out.println("Name ="+getName());
 System.out.println("Salary ="+getSalary());
 System.out.println("-------------");

 Iterator<Employee> employeeIterator = employees.iterator();
 while(employeeIterator.hasNext()){
 Employee employee = employeeIterator.next();
 employee.print();
 }
 }

 public void remove(Employee employee) {
 employees.remove(employee);
 }
}


class Developer implements Employee{

 private String name;
 private double salary;

 public Developer(String name,double salary){
 this.name = name;
 this.salary = salary;
 }
 public void add(Employee employee) {
 //this is leaf node so this method is not applicable to this class.
 }

 public Employee getChild(int i) {
 //this is leaf node so this method is not applicable to this class.
 return null;
 }

 public String getName() {
 return name;
 }

 public double getSalary() {
 return salary;
 }

 public String getDesignation(){
 return "Developer"; 
 }
 
 public void print() {
 System.out.println("-------------");
 System.out.println("Name ="+getName());
 System.out.println("Salary ="+getSalary());
 System.out.println("-------------");
 }

 public void remove(Employee employee) {
 //this is leaf node so this method is not applicable to this class.
 }

 }
 

public class CompositeEg {
 public static void main(String[] args) {
 try
 {
 Employee dir1 = new Director("dir1", 80000);
 Employee manager1=new Manager("mgr1",25000);
 
 Employee emp1=new Developer("dev1", 10000);
 Employee emp2=new Developer("dev2", 15000);
 
 manager1.add(emp1);
 manager1.add(emp2);
 
 dir1.add(manager1);
 
 Employee gManager=new Manager("mgr2", 50000);
 Employee emp3=new Developer("dev3", 20000);
 
 gManager.add(emp3);
 gManager.add(manager1);
 
 dir1.add(gManager);
 dir1.print();
 
 dir1.add( new Developer("ldev",19000));
 
 System.out.println("Designation: "+gManager.getDesignation());
 }catch(Exception e)
 {
 e.printStackTrace();
 }
 }
}

Design Pattern Free PDF