標籤:
Composite定義:將對象以樹形結構組織起來,以達成“部分-整體” 的階層,使得用戶端對單個對象和組合對象的使用具有一致性。
Composite比較容易理解,想到Composite就應該想到樹形結構圖。組合體內這些對象都有共同介面,當組合體一個對象的方法被調用執行時,Composite將遍曆(Iterator)整個樹形結構,尋找同樣包含這個方法的對象並實現調用執行。可以用牽一動百來形容。
所以Composite模式使用到Iterator模式,和Chain of Responsibility模式類似。
Composite好處:
- 使用戶端調用簡單,用戶端可以一致的使用組合結構或其中單個對象,使用者就不必關係自己處理的是單個對象還是整個組合結構,這就簡化了用戶端代碼。
- 更容易在組合體內加入對象組件。用戶端不必因為加入了新的對象組件而更改代碼。
如何使用Composite首先定義一個介面或抽象類別,這是設計模式通用方式了,其他設計模式對介面內部定義限制不多,Composite卻有個規定,那就是要在介面內部定義一個用於訪問和管理Composite組合體的對象們(或稱組件Component)。
下面的代碼是以抽象類別定義,一般盡量用介面interface。public abstract class Equipment{
private String name;
//網路價格
public abstract double netPrice();
//折扣價格
public abstract double discountPrice();
//增加組件方法
public boolean add(Equipment equipment) { return false; }
//刪除組件方法
public boolean remove(Equipment equipment) { return false; }
//注意這裡,這裡就提供一種用於訪問組合體類的組件方法。
public Iterator iter() { return null; }
public Equipment(final String name) { this.name=name; }
}抽象類別Equipment就是Component定義,代表著組合體類的對象們,Equipment中定義幾個共同的方法。public class Disk extends Equipment{
public Disk(String name) { super(name); }
//定義Disk網路價格為1
public double netPrice() { return 1.; }
//定義了disk折扣價格是0.5 對摺。
public double discountPrice() { return .5; }
}Disk是組合體內的一個對象,或稱一個組件,這個組件是個單獨元素( Primitive)。
還有一種可能是,一個組件也是一個組合體,就是說這個組件下面還有‘兒子‘,這是樹形結構中通常的情況,應該比較容易理解。現在我們先要定義這個組合體:abstract class CompositeEquipment extends Equipment{
private int i=0;
//定義一個Vector 用來存放‘兒子‘
private Lsit equipment=new ArrayList();
public CompositeEquipment(String name) { super(name); }
public boolean add(Equipment equipment) {
this.equipment.add(equipment);
return true;
}
public double netPrice(){
double netPrice=0.;
Iterator iter=equipment.iterator();
for(iter.hasNext())
netPrice+=((Equipment)iter.next()).netPrice();
return netPrice;
}
public double discountPrice(){
double discountPrice=0.;
Iterator iter=equipment.iterator();
for(iter.hasNext())
discountPrice+=((Equipment)iter.next()).discountPrice();
return discountPrice;
}
//注意這裡,這裡就提供用於訪問自己組合體內的組件方法。
//上面dIsk 之所以沒有,是因為Disk是個單獨(Primitive)的元素.
public Iterator iter(){
return equipment.iterator() ;
}
//重載Iterator方法
public boolean hasNext() { return i<equipment.size(); }
//重載Iterator方法
public Object next(){
if(hasNext())
return equipment.elementAt(i++);
else
throw new NoSuchElementException();
}
}上面CompositeEquipment繼承了Equipment,同時為自己裡面的對象們提供了外部存取的方法,重載了Iterator,Iterator是Java的Collection的一個介面,是Iterator模式的實現。
我們再看看CompositeEquipment的兩個具體類:盤盒Chassis和箱子Cabinet,箱子裡面可以放很多東西,如底板,電源盒,硬碟盒等;盤盒裡面可以放一些小裝置,如硬碟 軟碟機等。無疑這兩個都是屬於組合體性質的。public class Chassis extends CompositeEquipment{
public Chassis(String name) { super(name); }
public double netPrice() { return 1.+super.netPrice(); }
public double discountPrice() { return .5+super.discountPrice(); }
}
public class Cabinet extends CompositeEquipment{
public Cabinet(String name) { super(name); }
public double netPrice() { return 1.+super.netPrice(); }
public double discountPrice() { return .5+super.discountPrice(); }
}至此我們完成了整個Composite模式的架構。
我們可以看看用戶端調用Composote代碼:Cabinet cabinet=new Cabinet("Tower");
Chassis chassis=new Chassis("PC Chassis");
//將PC Chassis裝到Tower中 (將盤盒裝到箱子裡)
cabinet.add(chassis);
//將一個10GB的硬碟裝到 PC Chassis (將硬碟裝到盤盒裡)
chassis.add(new Disk("10 GB"));
//調用 netPrice()方法;
System.out.println("netPrice="+cabinet.netPrice());
System.out.println("discountPrice="+cabinet.discountPrice());上面調用的方法netPrice()或discountPrice(),實際上Composite使用Iterator遍曆了整個樹形結構,尋找同樣包含這個方法的對象並實現調用執行。
Composite是個很巧妙體現智慧的模式,在實際應用中,如果碰到樹形結構,我們就可以嘗試是否可以使用這個模式。
以論壇為例,一個版(forum)中有很多文章(message),這些文章有原始貼,有對原始貼的回應貼,是個典型的樹形結構,那麼當然可以使用Composite模式,那麼我們進入Jive中看看,是如何?的.Jive解剖在Jive中 ForumThread是ForumMessages的容器container(組合體)。也就是說,ForumThread類似我們上例中的 CompositeEquipment.它和messages的關係如下所示:
[thread]
|- [message]
|- [message]
|- [message]
|- [message]
|- [message]
我們在ForumThread看到如下代碼:public interface ForumThread {
....
public void addMessage(ForumMessage parentMessage, ForumMessage newMessage)
throws UnauthorizedException;
public void deleteMessage(ForumMessage message)
throws UnauthorizedException;
public Iterator messages();
....
}類似CompositeEquipment,提供用於訪問自己組合體內的組件方法:增加、刪除、遍曆。
結合我的其他模式中對Jive的分析,我們已經基本大體理解了Jive論壇體系的架構,如果你之前不理解設計模式,而直接去看Jive原始碼,你肯定無法看懂。
組合模式 概述
將對象組合成樹形結構以表示"部分-整體"的階層。"Composite使得使用者對單個對象和組合對象的使用具有一致性。"
適用性
1.你想表示對象的部分-整體階層。 2.你希望使用者忽略組合對象與單個對象的不同,使用者將統一地使用組合結構中的所有對象。
參與者
1.Component 為組合中的對象聲明介面。 在適當的情況下,實現所有類共有介面的預設行為。 聲明一個介面用於訪問和管理Component的子組件。 (可選)在遞迴結構中定義一個介面,用於訪問一個父組件,並在合適的情況下實現它。 2.Leaf 在組合中表示分葉節點對象,分葉節點沒有子節點。 在組合中定義節點對象的行為。 3.Composite 定義有子組件的那些組件的行為。 儲存子組件。 在Component介面中實現與子組件有關的操作。 4.Client 通過Component介面操縱組合組件的對象。
類圖 例子Component
public abstract class Employer { private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public abstract void add(Employer employer); public abstract void delete(Employer employer); public List employers; public void printInfo() { System.out.println(name); } public List getEmployers() { return this.employers; }}Leaf
public class Programmer extends Employer { public Programmer(String name) { setName(name); employers = null;//程式員, 表示沒有下屬了 } public void add(Employer employer) { } public void delete(Employer employer) { }}
public class ProjectAssistant extends Employer { public ProjectAssistant(String name) { setName(name); employers = null;//項目助理, 表示沒有下屬了 } public void add(Employer employer) { } public void delete(Employer employer) { }}Composite
public class ProjectManager extends Employer { public ProjectManager(String name) { setName(name); employers = new ArrayList(); } public void add(Employer employer) { employers.add(employer); } public void delete(Employer employer) { employers.remove(employer); }}Client
public class Test { public static void main(String[] args) { Employer pm = new ProjectManager("專案經理"); Employer pa = new ProjectAssistant("項目助理"); Employer programmer1 = new Programmer("程式員一"); Employer programmer2 = new Programmer("程式員二"); pm.add(pa);//為專案經理添加項目助理 pm.add(programmer2);//為專案經理添加程式員 List ems = pm.getEmployers(); for (Employer em : ems) { System.out.println(em.getName()); } }}result
項目助理程式員二
Java組合模式(Composite模式)