1. 程式人生 > >設計模式(十四)——組合模式

設計模式(十四)——組合模式

其他 獲取 添加 http es2017 處理方式 src 所有 alt

1.描述

將對象組合成樹形結構一以表示“部分——整體”的層次結構。組合模式使用戶對單個對象和組合對象的使用具有一致性。

2.模式的使用

·抽象組件(Component):是一個接口或抽象類,該接口定義了個體對象和組合對象需要實現的關於操作器子節點的方法,比如添加add()、刪除remove()、等方法。

·Composite節點(Composite Node):實現Component接口類的實例,節點不但實現Component接口,也可以包含其他Composite節點或Leaf節點的引用

·Leaf節點(Leaf Node):實現Component接口的類的實例,Leaf節點實現Component

節點,不可以包含其他Composite節點或Leaf節點的引用。

3.使用情景

·當想表示對象的部分——整體層次結構。

·希望用戶用一致的處理方式處理個體對象和組合對象。

4.優點

·組合模式包含個體對象和組合對象,並形成樹形結構,使用戶可以方便的處理個體對象和組合對象。

·組合對象和個體對象吧實現了統一接口,用戶一般無需區分個體對象和組合對象。

·當增加新的Conposite節點或時,用戶的重要代碼不需要修改。

5.UML

技術分享

6案例

某軍隊的編制中,連長下面兩個排長,排長下面三個班長,班長下面10個士兵。使用組合模式將這些對象構造成屬性結構(一些姓名、工資屬性不是重點)。

  1 package
組合模式; 2 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 6 public class test1 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 } 12 13 public static double computeSalary(MilitaryPerson person){//計算該單位下所有成員工資總和
14 //使用的遞歸 15 double sum = 0; 16 if(person.isLeaf()) 17 sum += person.getSalary(); 18 19 if(!person.isLeaf()){ 20 sum += person.getSalary(); 21 Iterator<MilitaryPerson> iterator = person.getAllChildren(); 22 while(iterator.hasNext()){ 23 MilitaryPerson p = iterator.next(); 24 sum = sum + computeSalary(p); 25 } 26 } 27 return sum; 28 } 29 } 30 31 /* 32 * 抽象組件 33 */ 34 interface MilitaryPerson{ 35 public void add(MilitaryPerson person);//添加成員 36 public void remove(MilitaryPerson person);//刪除成員 37 public MilitaryPerson getChild(int index);//按下表獲取成員 38 public Iterator<MilitaryPerson> getAllChildren();//獲取該單位下面所有成員的遍歷叠代器 39 public boolean isLeaf();//判斷是否為Leaf節點 40 public double getSalary(); 41 public void setSalary(double salary); 42 } 43 44 /* 45 * composite節點——班長、排長、連長 46 */ 47 class MilitaryOfficer implements MilitaryPerson{ 48 LinkedList<MilitaryPerson> list; 49 String name; 50 double salary; 51 MilitaryOfficer(String name, double salary){ 52 this.name = name; 53 this.salary = salary; 54 list = new LinkedList<MilitaryPerson>(); 55 } 56 public void add(MilitaryPerson person) { 57 list.add(person); 58 } 59 60 public void remove(MilitaryPerson person) { 61 list.remove(person); 62 } 63 64 public MilitaryPerson getChild(int index) { 65 return list.get(index); 66 } 67 68 public Iterator<MilitaryPerson> getAllChildren() { 69 return list.iterator(); 70 } 71 72 public boolean isLeaf() { 73 return false; 74 } 75 76 public double getSalary() { 77 return salary; 78 } 79 80 public void setSalary(double salary) { 81 this.salary = salary; 82 } 83 84 } 85 86 /* 87 * Leaf節點——士兵 88 */ 89 class MilitarySoldier implements MilitaryPerson{ 90 double salary; 91 String name; 92 MilitarySoldier(double salary, String name){ 93 this.salary = salary; 94 this.name = name; 95 } 96 public void add(MilitaryPerson person) {} 97 98 public void remove(MilitaryPerson person) {} 99 100 public MilitaryPerson getChild(int index) {return null;} 101 102 public Iterator<MilitaryPerson> getAllChildren() {return null;} 103 104 public boolean isLeaf() {return true;} 105 106 public double getSalary() { 107 return salary; 108 } 109 110 public void setSalary(double salary) { 111 this.salary = salary; 112 } 113 114 }

大概結構就是這樣,如果演示的話需要創建一堆士兵和長官的對象,太麻煩。

設計模式(十四)——組合模式