Magazine
 
Structural Design Patterns
 

Manager.java:

class Manager {
Manager manager;
Employee[] emply;
String dept;
Manager(Manager mgr,Employee[] e, String d ) {
this(e, d);
this.manager = mgr;
}

Manager(Employee[] e, String d) {
emply = e;
dept =d;
}
String getDept() {
return dept;
}
Manager getManager() {
return manager;
}
Employee[] getEmployee() {
return emply;
}
public String toString() {
return dept + ” manager”;
}}

Here is the “Test” class that shows the information about the “Manager” class. Test.java:

class Test {
public static void main(String[] args) {
Employee[] e1 = {new Employee(“Zulfiqar”, 90),
new Employee(“Amit”, 80)};
Manager m1 = new Manager(e1, ”Accounting”);

 

Employee[] e2 = {new Employee(“Aquil”, 70),
new Employee(“Ravi”, 60),
new Employee(“Vinod”, 40)};
Manager m2 = new Manager(m1, e2, ”Production”) ;

System.out.println(m2);
Employee[] emp = m2.getEmployee();
if (emp != null)
for (int k = 0; k < emp.length; k++)
System.out.println
(“ ”+emp[k]+” Salary: $”+ emp[k].getSalary());
Manager m = m2.getManager();
System.out.println(“ ” + m);
if (m!= null) {
Employee[] emps = m.getEmployee();
if (emps != null)
for (int k = 0; k < emps.length; k++)
System.out.println
(“ ” + emps[k]+” Salary: $”+ emps[k].getSalary());
}}}

Here is the output of the program:

C:\> java Test
Production manager
Employee Zulfiqar Salary: $90.0
Employee Amit Salary: $90.0
Employee Aquil Salary: $80.0
Accounting manager
Employee Ravi Salary: $70.0
Employee Vinod Salary: $50.0

The above example concludes that the composite pattern allows us to create a tree like structure for both simple and complex objects.

Oct 2007 | Java Jazz Up | 54
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF