JAVA的23種設計模式---組合模式
阿新 • • 發佈:2019-02-12
概要:
該文章參考了《設計模式之禪》一書及一些前輩的部落格文章
1.該文章闡述了組合模式的基礎原理及示例程式碼;
2.該文章適合初學設計模式的技術人員研習;
3.該文章有許多不足之處,請各位大咖指正,噴子繞道;
正文:
組合模式(合成模式、部分-整體模式):將物件組合成樹形結構以表示“部分-整體”的層次結構,使得使用者對單個物件和組合物件的使用具有一致性。
1.(安全模式)通用組合模式模板程式碼實現:
package com.csdn;
/**
* 抽象構件
* @author Administrator
*
*/
public abstract class Component {
//個體和整體都具有的共享
public void doSomething(){
//業務邏輯
}
}
package com.csdn;
import java.util.ArrayList;
/**
* 樹枝構件
* @author Administrator
*
*/
public class Composite extends Component{
//構件容器
private ArrayList<Component> componentArrayList = new ArrayList<Component>();
//增加一個葉子構件或樹枝構件
public void add(Component component){
this.componentArrayList.add(component);
}
//刪除一個葉子構件或樹枝構件
public void remove(Component component){
this.componentArrayList.remove(component);
}
//獲得分支下所有葉子構件或樹枝構件
public ArrayList<Component> getChildren(){
return this.componentArrayList;
}
}
package com.csdn;
/**
* 樹葉構件
* @author Administrator
*
*/
public class Leaf extends Component{
public void doSomething(){
//可以重寫父類方法
}
}
package com.csdn;
/**
* 模擬場景
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
//建立一個根節點
Composite root = new Composite();
root.doSomething();
//建立一個樹枝構件
Composite branch = new Composite();
//建立一個葉子節點
Leaf leaf = new Leaf();
//建立整體
root.add(branch);
branch.add(leaf);
}
//通過遞迴遍歷樹
public static void display(Composite root){
for(Component c:root.getChildren()){
if(c instanceof Leaf){
c.doSomething();
}else{
display((Composite)c);
}
}
}
}
注:
a:組合模式包含了抽象構件角色、葉子構件、樹枝構件這三種角色
b:安全模式的組合模式破壞了依賴倒轉原則
c:只要是樹形結構,只要是提現區域性和整體關係且關係較深的時候,就考慮組合模式
d:組合模式的真實引用,它依靠了關係資料庫的非物件儲存效能,非常方便的儲存了一個樹形結構。
e:安全模式把樹枝節點和樹葉節點徹底分開,樹枝節點擁有單獨用來組合的方法,這種方法比較安全。
2.(透明模式)通用組合模式模板程式碼實現:
package com.csdn2;
import java.util.ArrayList;
/**
* 抽象構件
* @author Administrator
*
*/
public abstract class Component {
//個體和整體都具有的共享
public void doSomething(){
//業務邏輯
}
//增加一個葉子構件或樹枝構件
public abstract void add(Component component);
//刪除一個葉子構件或樹枝構件
public abstract void remove(Component component);
//獲得分支下所有葉子構件或樹枝構件
public abstract ArrayList<Component> getChildren();
}
package com.csdn2;
import java.util.ArrayList;
/**
* 樹枝構件
* @author Administrator
*
*/
public class Composite extends Component{
//構件容器
private ArrayList<Component> componentArrayList = new ArrayList<Component>();
//增加一個葉子構件或樹枝構件
public void add(Component component){
this.componentArrayList.add(component);
}
//刪除一個葉子構件或樹枝構件
public void remove(Component component){
this.componentArrayList.remove(component);
}
//獲得分支下所有葉子構件或樹枝構件
public ArrayList<Component> getChildren(){
return this.componentArrayList;
}
}
package com.csdn2;
import java.util.ArrayList;
/**
* 樹葉節點
* @author Administrator
*
*/
public class Leaf extends Component{
//空實現,拋不支援請求異常
@Deprecated
@Override
public void add(Component component) throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public void remove(Component component) throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public ArrayList<Component> getChildren() throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
}
package com.csdn2;
/**
* 模擬場景
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
//建立一個根節點
Composite root = new Composite();
root.doSomething();
//建立一個樹枝構件
Composite branch = new Composite();
//建立一個葉子節點
Leaf leaf = new Leaf();
//建立整體
root.add(branch);
branch.add(leaf);
}
//通過遞迴遍歷樹
public static void display(Component root){
for(Component c:root.getChildren()){
if(c instanceof Leaf){
c.doSomething();
}else{
display(c);
}
}
}
}
注:
a:@Deprecated註解用來告訴呼叫者呼叫該方法可能會出錯
b:透明模式的組合模式遵循了依賴倒轉原則,但不安全
c:組合模式從下級向上級遍歷,需要在抽象構件中增加setParent和getParent兩個方法分別來設定父節點和獲取父節點,在樹枝和樹葉節點set其父節點