組合模式也稱爲部分整體模式,結構型設計模式之一,組合模式比較簡單,它將一組相似的對象看作一個對象處理,並根據一個樹狀結構來組合對象,然後提供一個統一的方法去訪問相應的對象,以此忽略掉對象與對象集合之間的差別。
組合模式的定義
將對象組合成樹形結構以表示“部分-整體”的層次結構,使得用戶對單個對象和組合對象的使用具有一致性。
使用場景
表示對象的部分-整體層次結構時
從一個整體中能夠獨立出部分模塊或功能的場景
組合模式的UML類圖
注意:組合模式分爲透明模式和安全模式,透明的組合模式是指將組合所使用到的方法定義在抽象類的方式。安全模式是正好相反。這裏的UML類圖爲透明的組合模式圖。
角色介紹:
抽象構件角色(component):是組合中的對象聲明接口,在適當的情況下,實現所有類共有接口的默認行爲。聲明一個接口用於訪問和管理Component子部件。
這個接口可 以用來管理所有的子對象。(可選)在遞歸結構中定義一個接口,用於訪問一個父部件,並在合適的情況下實現它。
樹葉構件角色(Leaf):在組合樹中表示葉節點對象,葉節點沒有子節點。並在組合中定義圖元對象的行爲。
樹枝構件角色(Composite):定義有子部件的那些部件的行爲。存儲子部件。在Component接口中實現與子部件有關的操作。
客戶角色(Client):通過component接口操縱組合部件的對象。
源碼的簡單實現
Component.java類,透明組合模式的抽象根節點
//透明的組合模式抽象根節點
public abstract class Component {
protected String name; // 節點名
public Component(String name) {
this.name = name;
}
// 具體的邏輯方法由子類實現
public abstract void doSomething();
//添加子節點
public abstract void addChild(Component child);
// 移除子節點
public abstract void removeChild(Component child);
// 獲取子節點
public abstract Component getChildren(int index);
}
Composite.java類,透明組合模式具體枝幹節點
// 透明組合模式具體枝幹節點
public class Composite extends Component {
// 存儲節點的容器
private List<Component> components = new ArrayList<>();
public Composite(String name) {
super(name);
}
@Override
public void doSomething() {
System.out.println(name);
if(null != components) {
for (Component c : components){
c.doSomething();
}
}
}
@Override
public void addChild(Component child) {
components.add(child);
}
@Override
public void removeChild(Component child) {
components.remove(child);
}
@Override
public Component getChildren(int index) {
return components.get(index);
}
}
Leaf.java類,透明的組合模式葉子節點
// 透明的組合模式葉子節點
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void doSomething() {
System.out.println(name);
}
@Override
public void addChild(Component child) {
throw new UnsupportedOperationException("葉子節點沒有子節點");
}
@Override
public void removeChild(Component child) {
throw new UnsupportedOperationException("葉子節點沒有子節點");
}
@Override
public Component getChildren(int index) {
throw new UnsupportedOperationException("葉子節點沒有子節點");
}
}
Client.java類,客戶端
public class Client {
public static void main(String[] args){
// 構造一個根節點
Composite root = new Composite("根節點");
// 構造兩個枝幹節點
Composite branch1 = new Composite(" 枝幹1");
Composite branch2 = new Composite(" 枝幹2");
// 構造兩個葉子節點
Leaf leaf1 = new Leaf(" 葉子1");
Leaf leaf2 = new Leaf(" 葉子2");
Leaf leaf3 = new Leaf(" 葉子3");
// 將葉子節點添加到枝幹節點中
branch1.addChild(leaf1);
branch1.addChild(leaf2);
branch2.addChild(leaf3);
// 將枝幹節點添加到根節點中
root.addChild(branch1);
root.addChild(branch2);
// 執行方法
root.doSomething();
}
}
輸出結果:
根節點
枝幹1
葉子1
葉子2
枝幹2
葉子3
組合模式的總結
組合模式的優點:
- 組合模式可以清楚地定義分層次的複雜對象,表示對象的全部或部分層次,它讓高層模塊忽略了層次的差異,方便對整個層次結構進行控制。
- 高層模塊可以一致地使用一個組合結構或其中單個對象,不必關心處理的是單個對象還是整個組合結構,簡化了高層模塊的代碼。
- 在組合模式中增加新的枝幹構件和葉子構件都很方便,無需對現有類庫進行任何修改,符合“開閉原則”。
- 組合模式爲樹形結構的面向對象實現提供了一種靈活的解決方案,通過葉子對象和枝幹對象的遞歸組合,可以形成複雜的樹形結構,但對樹形結構的控制卻非常簡單。
組合模式的缺點:
- 在新增構件時不好對枝幹中的構件類型進行限制,不能依賴類型系統來施加這些約束,因爲在大多數情況下,它們都來自於相同的抽象層,此時,必須進行類型檢查來實現,這個實現過程較爲複雜。