隨筆-348  評論-598  文章-0  trackbacks-0

          橋接模式,引用“四人幫的話”就是 “decouple an abstraction from its implementation so that the two can vary independently”,抽象與行為分離。

          說個例子

          客戶想畫圖,畫兩種圓和矩形,每種矩形都要調用特別的劃線(draw_a_line())方法,每一種圓也是。如果像上面那樣,是不是感覺混亂了點?看看下面的呢。

          這個是不是舒服點?的確,圖形和畫法都存在一一對應的關系,我們完全可以把圖形這個抽象和畫法這個行為分離,而不需要一個畫法對應多個抽象,如上面那個。

          public abstract class Shape
          {
              
          public abstract void draw();
          }


          //畫點和圓的方法
          public abstract class ShapeDrawing
          {
              
          public abstract void draw_a_line();
              
          public abstract void draw_a_circle();
          }


          public class ShapeDrawingSingleton
          {
              
          private static ShapeDrawing shapedrawingSingleton;
              
              
          public ShapeDrawingSingleton(ShapeDrawing shapedrawing)
              
          {
                  shapedrawingSingleton
          =shapedrawing;
              }

              
              
          public static ShapeDrawing getShapeDrawing()
              
          {
                  
          return shapedrawingSingleton;
              }

          }


          //畫圖方法基類
          public class VShape extends Shape
          {
              
          public ShapeDrawing shapedrawing;
              
              
          public void setShapeDrawing()
              
          {
                  
          this.shapedrawing=ShapeDrawingSingleton.getShapeDrawing();
              }

              
              
          public ShapeDrawing getShapeDrawing()
              
          {
                  
          return this.shapedrawing;
              }

          }


          public class V1Circle extends VShape
          {
              
          public Circle(){setShapeDrawing();}
              
              
          public void draw()
              
          {
                  
          //用shapedrawing畫圓
                  
          //shapedrawing.draw_a_circle()
              }

          }


          //
          //V1Rectangle和V2系列參照上面的
          //

          public class DP1ShapeDrawing extends ShapeDrawing
          {
              
          public void draw_a_line(){};
              
          public void draw_a_circle(){};
          }


          public class DP2ShapeDrawing extends ShapeDrawing
          {
              
          public void draw_a_line(){};
              
          public void draw_a_circle(){};
          }


          ShapeDrawingSingleton shapedrawingSingleton
          =new ShapeDrawingSingleton(new DP1ShapeDrawing());

          V1Circle v1circle
          =new V1Circle();
          v1circle.draw();

          //其他的類似于上面的




          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2007-04-08 14:27 TiGERTiAN 閱讀(1184) 評論(2)  編輯  收藏 所屬分類: Design Patterns

          評論:
          # re: Bridge Pattern[未登錄] 2007-04-08 16:19 | yy
          圖片看不到啊  回復  更多評論
            
          # re: Bridge Pattern 2007-04-08 19:53 | TiGERTiAN
          對不起,可以了!嘿嘿  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 会昌县| 长武县| 丽水市| 谷城县| 石阡县| 营山县| 长岛县| 浦城县| 浦东新区| 阿拉善左旗| 新津县| 库车县| 潮州市| 平定县| 德格县| 卓尼县| 扶风县| 繁昌县| 玉溪市| 安平县| 张家界市| 饶阳县| 连南| 溧阳市| 高淳县| 介休市| 凤台县| 灌南县| 翼城县| 浮梁县| 赣州市| 济阳县| 登封市| 象山县| 徐州市| 武清区| 琼结县| 黄山市| 龙陵县| 庆城县| 仪陇县|