Core Java Design Pattern : Structural Pattern : Composite Design Pattern
Composite Design Pattern |
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Use the Composite pattern when
- you want to represent part-whole hierarchies of objects.
- you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.
For the graphics system, this class is Graphic. Graphic declares operations like Draw that are specific to graphical objects. It also declares operations that all composite objects share, such as operations for accessing and managing its children.
The subclasses Line, Rectangle, and Text (see preceding class diagram) define primitive graphical objects. These classes implement Draw to draw lines, rectangles, and text, respectively. Since primitive graphics have no child graphics, none of these subclasses implements child-related operations.
The Picture class defines an aggregate of Graphic objects. Picture implements Draw to call Draw on its children, and it implements child-related operations accordingly. Because the Picture interface conforms to the Graphic interface, Picture objects can compose other Pictures recursively.
Examples |
The employees of a company are at various positions. Now, say in a hierarchy, the manager has subordinates; also the Project Leader has subordinates, i.e. employees reporting to him/her. The developer has no subordinates.
Employee.java
package com.javaskool;
import java.util.Vector;
public class Employee {
private String name;
private double salary;
private Vector subordinates; //Employee has subordinates.
//constructor
public Employee(String name, double sal) {
setName(name);
setSalary(sal);
subordinates = new Vector();
}
public String toString()
{
String empDetails="Name :- "+name+" Salary:- "+
salary+"\n Subordinates :- "+subordinates;
return empDetails;
}
public Vector getSubordinates() {
return subordinates;
}
public void setSubordinates(Vector subordinates) {
this.subordinates = subordinates;
}
// constructor
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void add(Employee e) {
subordinates.addElement(e);
}
public void remove(Employee e) {
subordinates.remove(e);
}
}
TestDrive.java
package com.javaskool;
public class TestDrive {
public static void main(String[] args) {
Employee CFO = new Employee("CFO", 30000);//CFO :- Chief Finance Officers
Employee headFinance1 = new Employee("Head Finance. North Zone", 20000);
Employee headFinance2 = new Employee("Head Finance. West Zone", 22000);
Employee accountant1 = new Employee("Accountant1", 10000);
Employee accountant2 = new Employee("Accountant2", 9000);
Employee accountant3 = new Employee("Accountant3", 11000);
Employee accountant4 = new Employee("Accountant4", 12000);
CFO.add(headFinance1);
CFO.add(headFinance2);
headFinance1.add(accountant1);
headFinance1.add(accountant4);
headFinance2.add(accountant2);
headFinance2.add(accountant3);
System.out.println(CFO);
System.out.println();
System.out.println(headFinance1);
System.out.println();
System.out.println(headFinance2);
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
Name :- CFO Salary:- 30000.0
Subordinates :- [Name :- Head Finance. North Zone Salary:- 20000.0
Subordinates :- [Name :- Accountant1 Salary:- 10000.0
Subordinates :- [], Name :- Accountant4 Salary:- 12000.0
Subordinates :- []], Name :- Head Finance. West Zone Salary:- 22000.0
Subordinates :- [Name :- Accountant2 Salary:- 9000.0
Subordinates :- [], Name :- Accountant3 Salary:- 11000.0
Subordinates :- []]]
Name :- Head Finance. North Zone Salary:- 20000.0
Subordinates :- [Name :- Accountant1 Salary:- 10000.0
Subordinates :- [], Name :- Accountant4 Salary:- 12000.0
Subordinates :- []]
Name :- Head Finance. West Zone Salary:- 22000.0
Subordinates :- [Name :- Accountant2 Salary:- 9000.0
Subordinates :- [], Name :- Accountant3 Salary:- 11000.0
Subordinates :- []]
Recent Comments