組合模式是結(jié)構(gòu)型模式,因?yàn)樗鼊?chuàng)建了一組對(duì)象的樹結(jié)構(gòu)。
組合模式將一組對(duì)象視為單個(gè)對(duì)象。
組合模式使用一個(gè)類來表示樹結(jié)構(gòu)。
在組合模式中,我們創(chuàng)建一個(gè)包含自己對(duì)象的類的組。
以下代碼使用Employee類來演示組合模式。
import java.util.ArrayList; import java.util.List; class Employee { private String name; private String title; private List<Employee> subordinates; public Employee(String name,String title) { this.name = name; this.title = title; 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; } public String toString(){ return "Employee :[ Name : "+ name +", dept : "+ title +subordinates +" ]"; } } public class Main { public static void main(String[] args) { Employee CEO = new Employee("John","CEO"); Employee headSales = new Employee("Rob","Sales"); Employee headMarketing = new Employee("Mike","Marketing"); Employee programmer1 = new Employee("Lili","Programmer"); Employee programmer2 = new Employee("Bob","Programmer"); Employee tester1 = new Employee("Jack","Tester"); Employee tester2 = new Employee("Tom","Tester"); CEO.add(headSales); CEO.add(headMarketing); headSales.add(tester1); headSales.add(tester2); headMarketing.add(programmer1); headMarketing.add(programmer2); //print all employees of the organization System.out.println(CEO); for (Employee headEmployee : CEO.getSubordinates()) { System.out.println(headEmployee); for (Employee employee : headEmployee.getSubordinates()) { System.out.println(employee); } } } }
上面的代碼生成以下結(jié)果。
更多建議: