I must
first define composite
objects: objects that contain other objects;
You can
take real life example of an organization. It has Managers and under Managers,
there can be PL and under PL there can be developers.
Composite
lets client treat individual objects and compositions of objects uniformly
An everyday example is enumerating the contents
of a file folder. A folder may contain not only files, but subfolders as well.
An application designed to recursively display the list of all files in some
top-level folder can use a conditional statement to distinguish between files
and directories, and traverse down the directory tree to display the names of
files in the subfolders. A better approach is suggested by the Composite
pattern. In this approach, every folder item, be it a file, a subfolder, a
network printer, or a moniker for any directory element, is an instance of a
class that conforms to an interface offering a method to display a
user-friendly name of the element. In this case, the client application does
not have to treat each element differently, thereby reducing the complexity of
the application logic.
When to
use it:
- you want to
represent part-whole hierachies of objects.
- you want client
to be able to ignore difference between compositions of objects and
individual objects.Clients will treat all objects in the composite
structure uniformly.
Now for
the companies all are employees are equal. All are getting salaries and all will
follow same protocol. The over all idea is Composite lets client treat
individual objects and compositions of objects uniformly . ie all objects in
the tree structure are equal .in other words they will implement one common
interface.
public class Employee {
ArrayList<Employee>
employees = new ArrayList<Employee>();
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
// TODO
Auto-generated constructor stub
}
public void add(Employee employee) {
employees.add(employee);
}
public String getName() {
// TODO
Auto-generated method stub
return name;
}
public double getSalary() {
// TODO
Auto-generated method stub
return salary;
}
public void print() {
if (employees.size() == 0) {
System.err.println("Leaf
Name : " + name);
}
else {
System.err.println("Node
Name : " + name);
for (Employee employee : employees) {
employee.print();
}
}
}
}
Test this program
public class
TestCompostePatt2 {
public static void main(String[] args) {
Employee parikshit = new Employee("Parikshit", 16000);
Employee amit = new Employee("Amit", 16000);
Employee ritika = new Employee("Ritika", 16000);
Employee sumeet = new Employee("sumeet", 16000);
Employee navneet = new Employee("navneet", 16000);
Employee prashant = new Employee("Prashant", 16000);
Employee puneet = new Employee("Puneet", 16000);
Employee ankur = new Employee("Ankur", 16000);
Employee deepak = new Employee("Deepak", 16000);
navneet.add(parikshit);
navneet.add(ritika);
navneet.add(ankur);
Employee tarun = new Employee("Tarun", 32000);
tarun.add(prashant);
tarun.add(deepak);
tarun.add(navneet);
Employee vineet = new Employee("vineet", 32000);
vineet.add(sumeet);
vineet.add(puneet);
Employee groupLead = new Employee("Girjaa", 90000);
groupLead.add(tarun);
groupLead.add(vineet);
groupLead.print();
}
}
Output :
Node Name : Girjaa
Node Name : Tarun
Leaf Name : Prashant
Leaf Name : Deepak
Node Name : navneet
Leaf Name : Parikshit
Leaf Name : Ritika
Leaf Name : Ankur
Node Name : vineet
Leaf Name : sumeet
Leaf Name : Puneet
Here in this hierarchy every object is a employee object and that’s way they are equal
and we can treat them uniformly.
Now the
company wants separate PLs , GLs and Developers. Like the figure below :
So in order to make all objects uniform we
define a Employee Interface and all the Objects will implement that.
public interface Employee {
public void add(Employee employee);
public String getName();
public double getSalary();
public void print();
}
public class ProjectLead implements Employee {
ArrayList<Employee>
employees = new ArrayList<Employee>();
private String name;
private int salary;
public ProjectLead(String name, int salary) {
this.name = name;
this.salary = salary;
// TODO
Auto-generated constructor stub
}
public void add(Employee employee) {
employees.add(employee);
}
public String getName() {
// TODO
Auto-generated method stub
return name;
}
public double getSalary() {
// TODO
Auto-generated method stub
return salary;
}
public void print() {
{
System.err.println("PL
Name : " + name);
for (Employee employee : employees) {
employee.print();
}
}
}
}
public class TeamLead implements Employee {
ArrayList<Employee>
employees = new ArrayList<Employee>();
private String name;
private int salary;
public TeamLead(String name, int salary) {
this.name = name;
this.salary = salary;
// TODO
Auto-generated constructor stub
}
public void add(Employee employee) {
employees.add(employee);
}
public String getName() {
// TODO
Auto-generated method stub
return name;
}
public double getSalary() {
// TODO
Auto-generated method stub
return salary;
}
public void print() {
if (employees.size() == 0) {
System.err.println("Leaf
Name : " + name);
}
else {
System.err.println("Team
Lead : " + name);
for (Employee employee : employees) {
employee.print();
}
}
}
}
public class Developer implements Employee{
ArrayList<Employee>
employees = new ArrayList<Employee>();
private String name;
private int salary;
public Developer(String name, int salary) {
this.name = name;
this.salary = salary;
// TODO
Auto-generated constructor stub
}
public void add(Employee employee) {
employees.add(employee);
}
public String getName() {
// TODO
Auto-generated method stub
return name;
}
public double getSalary() {
// TODO
Auto-generated method stub
return salary;
}
public void print() {
if (employees.size() == 0) {
System.err.println("Developer
Name : " + name);
}
else {
System.err.println("Developer
Name : " + name);
for (Employee employee : employees) {
employee.print();
}
}
}
}
Test this program via this
public class TestCompostePatt {
public static void
main(String[] args) throws
InterruptedException {
Developer
parikshit = new
Developer("Parikshit", 16000);
Developer
amit = new
Developer("Amit", 16000);
Developer
ritika = new
Developer("Ritika", 16000);
Developer
sumeet = new
Developer("sumeet", 16000);
Developer
navneet = new
Developer("navneet", 16000);
Developer
prashant = new
Developer("Prashant", 16000);
Developer
puneet = new
Developer("Puneet", 16000);
Developer
ankur = new Developer("Ankur", 16000);
Developer
deepak = new
Developer("Deepak", 16000);
navneet.add(parikshit);
navneet.add(ritika);
navneet.add(ankur);
TeamLead
tarun = new
TeamLead("Tarun", 32000);
tarun.add(prashant);
tarun.add(deepak);
tarun.add(navneet);
TeamLead
vineet = new
TeamLead("vineet", 32000);
vineet.add(sumeet);
vineet.add(puneet);
ProjectLead
groupLead = new
ProjectLead("Girjaa", 90000);
groupLead.add(tarun);
groupLead.add(vineet);
groupLead.print();
Thread.sleep(500);
System.out.println("------------------------");
tarun.print();
}
}
PL Name : Girjaa
Team Lead : Tarun
Developer Name : Prashant
Developer Name : Deepak
Developer Name : navneet
Developer Name : Parikshit
Developer Name : Ritika
Developer Name : Ankur
Team Lead : vineet
Developer Name : sumeet
Developer Name : Puneet
------------------------
Team Lead : Tarun
Developer Name : Prashant
Developer Name : Deepak
Developer Name : navneet
Developer Name : Parikshit
Developer Name : Ritika
Developer Name : Ankur
Here all the three class are same except
one method Print( ) . Since PL and TL will have members in it ((employees.size() will
never be zero)
No comments:
Post a Comment