标签:
publicclassCompositePatternDemo{
publicstaticvoid main(String[] args){
Employee CEO =newEmployee("John","CEO",30000);
Employee headSales =newEmployee("Robert","Head Sales",20000);
Employee headMarketing =newEmployee("Michel","Head Marketing",20000);
Employee clerk1 =newEmployee("Laura","Marketing",10000);
Employee clerk2 =newEmployee("Bob","Marketing",10000);
Employee salesExecutive1 =newEmployee("Richard","Sales",10000);
Employee salesExecutive2 =newEmployee("Rob","Sales",10000);
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExecutive1);
headSales.add(salesExecutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
//打印该组织的所有员工
System.out.println(CEO);
for(Employee headEmployee : CEO.getSubordinates()){
System.out.println(headEmployee);
for(Employee employee : headEmployee.getSubordinates()){
System.out.println(employee);
}
}
}
}
import java.util.ArrayList;
import java.util.List;
publicclassEmployee{
privateString name;
privateString dept;
privateint salary;
privateList<Employee> subordinates;
//构造函数
publicEmployee(String name,String dept,int sal){
this.name = name;
this.dept = dept;
this.salary = sal;
subordinates =newArrayList<Employee>();
}
publicvoid add(Employee e){
subordinates.add(e);
}
publicvoid remove(Employee e){
subordinates.remove(e);
}
publicList<Employee> getSubordinates(){
return subordinates;
}
publicString toString(){
return("Employee :[ Name : "+ name
+", dept : "+ dept +", salary :"
+ salary+" ]");
}
}
标签:
原文地址:http://www.cnblogs.com/Doing-what-I-love/p/5621182.html