Monday, 1 September 2014

Composite Design Pattern Implementation

Composite Design Pattern

A composite object is an object designed as a composition of one-or-more similar type of objects, all showing similar functionality. For more details about Composite design pattern description please refer this link : http://www.javatechtipssharedbygaurav.com/2014/03/java-design-patterns-part-2.html



In a School, there are tree structure for employees. At top, there is a Director. There are two employees, School principal is reporting to director and Head Of admin Department is also reporting to director and further Pricipal has two teachers one is Math Teacher and another one is Physics Teacher. Now our target is to print employees details from top to bottom.

1. SchoolEmployee.java

package com.gaurav.designpattern.composite;

/**
 * @author gaurav
 * 
 */
public interface SchoolEmployee {
public String getEmployeeName();
public Double getEmployeeSalary();
public int getEmployeeId();
public String getEmployeeDesig();
public void getEmployeeDetails();
public int getHolidayLeaves();
public boolean addEmployee(SchoolEmployee schoolEmployee);
}

2. SchoolPricipal.java

package com.gaurav.designpattern.composite;

/**
 * @author gaurav
 * 
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SchoolPricipal implements SchoolEmployee{
List<SchoolEmployee> schoolEmployeeList = new ArrayList<SchoolEmployee>();
private int employeeId;
private String employeeName;
private Double employeeSalary;
private String employeeDesig;
private int holidayLeaves;
public SchoolPricipal(int empId, String name, Double salary, String desig){
this.employeeId = empId;
this.employeeName = name;
this.employeeSalary = salary;
this.employeeDesig = desig;
}
@Override
public void getEmployeeDetails() {
System.out.println("-----------------------------------");
System.out.println("Employee name is : " + getEmployeeName()
+ "\nSalary : " + getEmployeeSalary() + "\nID : "
+ getEmployeeId() + "\nDesig : " + getEmployeeDesig()
+ "\nHoliday Leaves : " + getHolidayLeaves());
System.out.println("-----------------------------------");
Iterator<SchoolEmployee> employeeIterator = schoolEmployeeList
.iterator();
while (employeeIterator.hasNext()) {
SchoolEmployee schoolEmployee = employeeIterator.next();
schoolEmployee.getEmployeeDetails();
}
}
@Override
public Double getEmployeeSalary() {
return employeeSalary;
}

@Override
public int getHolidayLeaves() {
holidayLeaves = 21;
return holidayLeaves;
}

@Override
public boolean addEmployee(SchoolEmployee schoolEmployee) {
boolean addStatus  = schoolEmployeeList.add(schoolEmployee);
return addStatus;
}

@Override
public String getEmployeeName() {
return employeeName;
}

@Override
public int getEmployeeId() {
return employeeId;
}

@Override
public String getEmployeeDesig() {
return employeeDesig;
}
}

3. SchoolTeacher.java

package com.gaurav.designpattern.composite;

/**
 * @author gaurav
 * 
 */
import java.util.ArrayList;
import java.util.List;

public class SchoolTeacher implements SchoolEmployee{
List<SchoolEmployee> schoolEmployeeList = new ArrayList<SchoolEmployee>();
private int employeeId;
private String employeeName;
private Double employeeSalary;
private String employeeDesig;
private int holidayLeaves;
public SchoolTeacher(int empId, String name, Double salary, String desig){
this.employeeId = empId;
this.employeeName = name;
this.employeeSalary = salary;
this.employeeDesig = desig;
}
@Override
public void getEmployeeDetails() {

System.out.println("-----------------------------------");
System.out.println("Employee name is : " + getEmployeeName()
+ "\nSalary : " + getEmployeeSalary() + "\nID : "
+ getEmployeeId() + "\nDesig : " + getEmployeeDesig()
+ "\nHoliday Leaves : " + getHolidayLeaves());
System.out.println("-----------------------------------");
}
@Override
public Double getEmployeeSalary() {
return employeeSalary;
}

@Override
public int getHolidayLeaves() {
holidayLeaves = 11;
return holidayLeaves;
}
@Override
public boolean addEmployee(SchoolEmployee schoolEmployee) {
boolean addStatus  = schoolEmployeeList.add(schoolEmployee);
return addStatus;
}

@Override
public String getEmployeeName() {
return employeeName;
}

@Override
public int getEmployeeId() {
return employeeId;
}

@Override
public String getEmployeeDesig() {
return employeeDesig;
}
}

4. CompositeDesignPatternDemo.java

package com.gaurav.designpattern.composite;

/**
 * @author gaurav
 * 
 */
public class CompositeDesignPatternDemo {

public static void main(String[] args) {
SchoolEmployee schoolEmployee1 = new SchoolTeacher(1, "Arjun Prasad",
19000.00, "Math Teacher");
SchoolEmployee schoolEmployee2 = new SchoolTeacher(7, "Deepti Mohanty",
25000.00, "Physics Teacher");
SchoolEmployee pricipal = new SchoolPricipal(1001, "Anurag Kumar", 55000.00,
"Pricipal");

pricipal.addEmployee(schoolEmployee1);
pricipal.addEmployee(schoolEmployee2);

SchoolEmployee schoolEmployee3 = new SchoolTeacher(10, "Kirti Ranjan", 56000.00,
"Head Of admin Department");
SchoolPricipal director = new SchoolPricipal(5001, "Gaurav Kumar", 95000.00,
"Trustee Head");
director.addEmployee(schoolEmployee3);
director.addEmployee(pricipal);
director.getEmployeeDetails();
}
}


Result :-

-----------------------------------
Employee name is : Gaurav Kumar
Salary : 95000.0
ID : 5001
Desig : Trustee Head
Holiday Leaves : 21
-----------------------------------
-----------------------------------
Employee name is : Kirti Ranjan
Salary : 56000.0
ID : 10
Desig : Head Of admin Department
Holiday Leaves : 11
-----------------------------------
-----------------------------------
Employee name is : Anurag Kumar
Salary : 55000.0
ID : 1001
Desig : Pricipal
Holiday Leaves : 21
-----------------------------------
-----------------------------------
Employee name is : Arjun Prasad
Salary : 19000.0
ID : 1
Desig : Math Teacher
Holiday Leaves : 11
-----------------------------------
-----------------------------------
Employee name is : Deepti Mohanty
Salary : 25000.0
ID : 7
Desig : Physics Teacher
Holiday Leaves : 11
-----------------------------------

No comments:

Post a Comment