Swing


          天行健 君子以自強不息

          posts - 69, comments - 215, trackbacks - 0, articles - 16
             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          設計模式:結構模式之Composite(組合)

          Posted on 2007-06-04 11:41 zht 閱讀(462) 評論(0)  編輯  收藏 所屬分類: 設計模式
          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是個很巧妙體現智慧的模式,在實際應用中,如果碰到樹形結構,我們就可以嘗試是否可以使用這個模式。

          主站蜘蛛池模板: 淮滨县| 枣阳市| 柘荣县| 中西区| 高唐县| 尚志市| 城固县| 常宁市| 本溪| 墨玉县| 章丘市| 盐源县| 卓资县| 邹平县| 霍邱县| 凤山县| 马龙县| 竹北市| 保靖县| 方城县| 锦屏县| 永清县| 南漳县| 建瓯市| 荥经县| 蒙山县| 洛宁县| 河津市| 克拉玛依市| 尤溪县| 静乐县| 丰原市| 福鼎市| 柳江县| 荣成市| 嘉黎县| 霍林郭勒市| 汪清县| 南安市| 宁安市| 瓮安县|