public class Employee{ private String name; private List<Employee> subordinates; public Employee(String name){ this.name = name; subordinates = new ArrayList<Employee>(); }
public void add(Employee e){ subordinates.add(e); }
public void remove(Employee e){ subordinates.remove(e); }
public List<Employee> getSubordinates(){ return subordinates; } }
很明显,组合者是一个树形的结构,每个组合者是一个树节点。
1 2 3 4 5 6 7
public static void main(String[] args){ Employee Boss = new Employee("boss"); Employee manager = new Employee("manager"); Boss.add(manager); Employee me = new Employee("me"); manager.add("me"); }