Composite模式及其在JSF中的應用
更新時間: 2007-05-24 11:42:57來源: 粵嵌教育瀏覽量:905
一 學習背景
在學習關于JSF組件時涉及到了composite模式,于是就查看一些資料,以下是自己對這種模式的理解。
二 自己整理的一些資料(見參考資料)
1.composite模式意在組成任意復雜度的整體--部分組件層次結構,同時將單個組件或復合組件視為統一的接口。樹形組織結構就是其中一種表現形式。
樹形結構中有葉子結點和非葉子結點(根結點是特例),非葉子結點可以添加,刪除(add(),delete())子結點,獲取子結點(getChild()),葉子結點沒有;此外樹結構的所有節點還有共同的操作(operator()).
用戶界面通常由兩種基本類型的組件構造:基本組件和容器組件,容器組件可以在其內部嵌套任意數目的組件,而基本組件則不行。使用這兩種組件類型,開發者可以建立更強大的組件,進而創建多姿多彩的用戶界面。
但是在與復雜的組件層次結構打交道時,必須在容器組件和基本組件之間進行區分,比較麻煩,composite提供了一種解決方案。適用它的情況:
a. 要表現“部分-整體”的層次結構時
b. 希望在事件組件層次中,同等對待復合組件與單個組件。
2. 通過下面的示例來理解
示例1:
基類shape 類有兩個派生類Circle和Square(相當于葉子結點或者是單個組件),第三個派生類CompositeShape是個組合體(相當于非葉子結點或者是容器組件),它持有一個含有多個shape實例的列表,當調用CompositeShape中的draw()時,它就把這個方法委托給列表中的每一個實例。
對于系統而言,一個CompositeShape實例就像是一個獨立的shape,可以把它傳給使用shape的方法或者對象。實際上,它只是一組shape實例的proxy.
程序:
Shape.java:
Public interface Shape {
Public void draw();
}
CompositeShape.java:
[code]Public class CompositeShape implements Shape {
private Vector Comshape = new Vector();
public void add(Shape shape) {
Comshape.add(shape);
}
Public void draw() {
for( int i = 0; i < comshape.size(); i ++ ) {
Shape shape = (Shape) comshape.elementAt(i);
Shape.draw();
}
}
}
示例2:
抽象類Equipment就是Component定義,代表著組合體類的對象們,Equipment中定義幾個共同的方法。
package com.interf;
public abstract class Equipment {
private String name;
private double netPrice;
private double discountPrice;
public Equipment(String name) {
this.name = name;
}
public abstract double netPrice();
public abstract double discountPrice();
}
Disk是組合體內的一個對象,或稱一個部件,這個部件是個單獨元素( Primitive)。
Disk.java:
package implEquip;
import com.interf.Equipment;
public class Disk extends Equipment {
public Disk(String name) {
super(name);
// TODO Auto-generated constructor stub
}
//定義Disk實價為1
public double netPrice() {
return 1.;
}
//定義了disk折扣價格是0.5 對折。
public double discountPrice() {
return .5;
}
}
還有一種可能是,一個部件也是一個組合體,就是說這個部件下面還有'兒子',這是樹形結構中通常的情況,應該比較容易理解。現在我們先要定義這個組合體:
CompsiteEquipment.java:
package implEquip;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import com.interf.Equipment;
public class CompositeEquipment extends Equipment {
private int i=0;
// 定義一個Vector 用來存放'兒子'
private List equipment = new ArrayList();
public CompositeEquipment(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public boolean add(Equipment equipment) {
this.equipment.add(equipment);
return true;
}
public double netPrice() {
double netPrice=0.;
Iterator iter=equipment.iterator();
while(iter.hasNext())
netPrice+=((Equipment)iter.next()).netPrice();
return netPrice;
}
public double discountPrice() {
double discountPrice=0.;
Iterator iter=equipment.iterator();
while(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.get(i++);
else
throw new NoSuchElementException();
}
}
上面CompositeEquipment繼承了Equipment,同時為自己里面的對象們提供了外部訪問的方法,重載了Iterator,Iterator是Java的Collection的一個接口,是Iterator模式的實現.
我們再看看CompositeEquipment的兩個具體類:盤盒Chassis和箱子Cabinet,箱子里面可以放很多東西,如底板,電源盒,硬盤盒等;盤盒里面可以放一些小設備,如硬盤 軟驅等。無疑這兩個都是屬于組合體性質的。
Cabinet.java:
package implEquip;
public class Cabinet extends CompositeEquipment {
public Cabinet(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public double netPrice() {
return 1.+super.netPrice();
}
public double discountPrice() {
return .5+super.discountPrice();
}
}
Chassi.java:
package implEquip;
public class Chassis extends CompositeEquipment {
public Chassis(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public double netPrice() {
return 1.+super.netPrice();
}
public double discountPrice() {
return .5+super.discountPrice();
}
}
至此我們完成了整個Composite模式的架構。我們可以看看客戶端調用Composote代碼:
CompositeTest.java:
package test;
import implEquip.Cabinet;
import implEquip.Chassis;
import implEquip.Disk;
public class CompositeTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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遍歷了整個樹形結構,尋找同樣包含這個方法的對象并實現調用執行.
控制臺輸出:
netPrice=3.0
discountPrice=1.5
三.JSF組件提供的實現非常接近Composite模式給出的一般性解決方案,不過(和Swing不同)這里的頂層和一般組件之間沒有明顯差異。
JSF提供了一個所有組件都要實現的UIComponent接口,UIComponentBase類為了方便組件開發者,定義了默認的行為。JSF有許多基本組件如UIForm和UIInput。另外可以創建自己的自定義組件。創建自定義組件時,必須要實現UIComponent接口或者繼承UIComponentBase類
JSF為使用默認組件提供了方便的標記庫。當頁面被應用的用戶提交時,FacesServlet 將根據這些標記所提供和搜集的信息實際建立一個組件樹(本人想法:具體建樹過程以及管理被封裝起來了,在管理和使用UI組件的原理應該同composite模式一致;實際是什么???能找到實現的代碼了)
層:form
第二層:label,outputtext,panel
第三層:command1 command2
UI組件是在服務器的視圖或者組件樹中進行管理,組件可以直接關系到JavaBean屬性的值(任何遵循了JavaBean命名約定的Java對象都可以為JSF組件命使用),在客戶端表現為HTML語言(或者其他顯示語言)。JavaBean用來收集用戶輸入的數據,并在需要時重新封閉到頁面中。
四.參考資料:
1 設計模式之Composite(組合) 板橋里人 http://www.jdon.com 2002/04/27
網址:http://www.jdon.com/designpatterns/composite.htm
2 《Mastering JavaServer Faces》中文版 (章 1.4)
3 《敏捷軟件開發:原則、模式與實踐》
4.《JSF實戰》