設計模式-12組合模式(Composite Pattern)

1.模式動機

很多時候會存在「部分-整體」的關係,例如:大學中的部門與學院、總公司中的部門與分公司、學慣用品中的書與書包。在軟體開發中也是這樣,例如,文件系統中的文件與文件夾、窗體程式中的簡單控制項與容器控制項等。對這些簡單對象與複合對象的處理,如果用組合模式來實現會很方便。

2.模式定義

組合(Composite)模式的定義:有時又叫作部分-整體模式,它是一種將對象組合成樹狀的層次結構的模式,用來表示「部分-整體」的關係,使用戶對單個對象和組合對象具有一致的訪問性。

屬於:結構型模式

組合模式分為透明式的組合模式安全式的組合模式

3.模式結構

組合模式包含以下主要角色:

  • 抽象構件(Component)角色:它的主要作用是為樹葉構件和樹枝構件聲明公共介面,並實現它們的默認行為。在透明式的組合模式中抽象構件還聲明訪問和管理子類的介面;在安全式的組合模式中不聲明訪問和管理子類的介面,管理工作由樹枝構件完成。
  • 樹葉構件(Leaf)角色:是組合中的葉節點對象,它沒有子節點,用於實現抽象構件角色中 聲明的公共介面。
  • 樹枝構件(Composite)角色:是組合中的分支節點對象,它有子節點。它實現了抽象構件角色中聲明的介面,它的主要作用是存儲和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。

透明式組合方式
在該方式中,由於抽象構建聲明了所有子類中的全部方法,所以客戶端無需區別樹葉對象和樹枝對象,對客戶端來說是透明的。
缺點是:樹葉構建本來沒有 add()、remove() 及 getChild() 等方法,卻要實現它們(空實現或拋異常),這樣會帶來一些安全性問題。

安全式組合方式
在該方式中,將管理自購件的方法移到樹枝構件中,抽象構件和樹葉構件沒有對子對象的管理方法,這樣就避免了上一種方式的安全性問題。
但由於葉子和分支有不同的介面,客戶端在調用時要知道樹葉對象和樹枝對象的存在,所以失去了透明性。

4.模式程式碼

透明式組合方式

# 抽象構件  public abstract class Component {      protected String name;        public Component(String name) {          this.name = name;      }        public abstract String name();        public boolean addChild(Component component) {          throw new UnsupportedOperationException("addChild not supported!");      }        public boolean removeChild(Component component) {          throw new UnsupportedOperationException("removeChild not supported!");      }        public Component getChild(int index) {          throw new UnsupportedOperationException("getChild not supported!");      }  }    # 樹枝構件  public class Composite extends Component {      private List<Component> componentList;        public Composite(String name) {          super(name);          componentList = new ArrayList<>();      }        @Override      public String name() {          StringBuilder builder = new StringBuilder(this.name);          for (Component component : componentList) {              builder.append("n");              builder.append(component.name());          }          return builder.toString();      }        @Override      public boolean addChild(Component component) {          return componentList.add(component);      }        @Override      public boolean removeChild(Component component) {          return componentList.remove(component);      }        @Override      public Component getChild(int index) {          return componentList.get(index);      }  }    # 樹葉構件  public class Leaf extends Component {      public Leaf(String name) {          super(name);      }        @Override      public String name() {          return name;      }  }    # Client  public class Client {      public static void main(String[] args) {          // 根節點          Component root = new Composite("root");          // 樹枝節點          Component branchA = new Composite("---branchA");          Component branchB = new Composite("------branchB");          // 樹葉節點          Component leafA = new Leaf("------leafA");          Component leafB = new Leaf("---------leafB");          Component leafC = new Leaf("---leafC");            root.addChild(branchA);          root.addChild(leafC);            branchA.addChild(leafA);          branchA.addChild(branchB);            branchB.addChild(leafB);            // 葉子節點不支援操作  //        leafC.addChild(null);            String result = root.name();          System.out.println(result);      }  }

按照透明式的字面意思,這裡應該沒有 Leaf 類,只有樹枝構件,樹枝構件既可以作為樹枝構件也可以作為樹葉構件,所以對客戶端來說是透明的。

安全式組合方式
安全式組合方式就是將透明式組合方式 Component 中的 add、remove 等方法放在樹枝節點中,抽象構件中之定義樹枝構件和樹葉構件中公共的部分。

# 抽象構件  public abstract class Component {      protected String name;        public Component(String name) {          this.name = name;      }        public abstract String name();  }    # 樹枝構件  public class Composite extends Component {      private List<Component> componentList;        public Composite(String name) {          super(name);          componentList = new ArrayList<>();      }        @Override      public String name() {          StringBuilder builder = new StringBuilder(this.name);          for (Component component : componentList) {              builder.append("n");              builder.append(component.name());          }          return builder.toString();      }        public boolean addChild(Component component) {          return componentList.add(component);      }        public boolean removeChild(Component component) {          return componentList.remove(component);      }        public Component getChild(int index) {          return componentList.get(index);      }  }    # 樹葉構件  public class Leaf extends Component {      public Leaf(String name) {          super(name);      }        @Override      public String name() {          return name;      }  }    # Client  public class Client {      public static void main(String[] args) {          // 根節點 這裡改為 Composite          Composite root = new Composite("root");          // 樹枝節點 這裡改為 Composite          Composite branchA = new Composite("---branchA");          Composite branchB = new Composite("------branchB");          // 樹葉節點          Leaf leafA = new Leaf("------leafA");          Leaf leafB = new Leaf("---------leafB");          Leaf leafC = new Leaf("---leafC");            root.addChild(branchA);          root.addChild(leafC);            branchA.addChild(leafA);          branchA.addChild(branchB);            branchB.addChild(leafB);            // 葉子節點不支援操作  //        leafC.addChild(null);            String result = root.name();          System.out.println(result);      }  }

樹葉構件時,客戶端需要區分樹枝構件(Composite)和樹葉構件(Leaf),所以失去了透明性。按照定義,透明式模式中就不應該有 Leaf 類的定義。
這樣的好處是 介面定義職責清晰,符合設計模式的 單一職責原則 和 介面隔離原則。

5.總結

優點

  • 組合模式使得客戶端程式碼可以一致地處理單個對象和組合對象,無須關心自己處理的是單個對象,還是組合對象,這簡化了客戶端程式碼;
  • 更容易在組合體內加入新的對象,客戶端不會因為加入了新的對象而更改源程式碼,滿足「開閉原則」;

缺點

  • 設計較複雜,客戶端需要花更多時間理清類之間的層次關係;
  • 不容易限制容器中的構件;
  • 不容易用繼承的方法來增加構件的新功能;

場景

  • 在需要表示一個對象整體與部分的層次結構的場合。
  • 要求對用戶隱藏組合對象與單個對象的不同,用戶可以用統一的介面使用組合結構中的所有對象的場合。