夏天到了

          雨季過后,是蘑菇生長的季節

           

          delegation(委托)

          Delegation 定義:
          Delegation is a way of making composition as powerful for reuse as inheritance [Lie86, JZ91]. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. This is analogous to subclasses deferring requests to parent classes. But with inheritance, an inherited operation can always refer to the receiving object through the this member variable in C++ and self in Smalltalk. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver

          我的理解:
          ??? A 把外界傳來的訊息" 轉送" B ﹐由B 處理之﹐我們稱A 委托B 。當一些事物互相溝通分工合作時常用妥托觀念。

          值得注意的是:
          delegate -----> delegatee
          ? ? ?^? ? ? ? ? ? ? ? ? |
          ? ? ?|? ?indirection? ? |
          ? ? ?--------------------
          delegate將自己(對象)傳給delegatee,使delegatee可以通過該對象執行那些委托的操作。

          舉個例子:
          ??? 在某個框架的組成部分中,對窗口設計,可以支持任意形狀的窗口。
          ??? 開始的時候考慮是不是使用子類來實現,乍一看似乎是比較好的,但是考慮到對窗口來說形狀只能是它的一項屬性:窗口不是一“種”形狀——窗口“有”形狀。(引自《The Pragmatic Programmer》)所以采用委托的方式。
          ??? 這時,我們定義一個被委托(delegatee)類Shape和一個委托(delegate)類window。
          public abstract class Shape{?????????????????
          ??? //....???????????????????????????????????
          ??? public abstract boolean overlaps(Shape s);
          ??? public abstract int getArea();???????????
          }????????????????????????????????????????????

          public class window{?????????????????????????
          ??? private Shape shape;?????????????????????
          ??? public window(Shape shape){??????????????
          ??????? this.shape = shape;??????????????????
          ??????? ...??????????????????????????????????
          ??? }?????????????????????????????????????????
          ??? public void setShape(Shape shape){???????
          ??????? this.shape = shape;??????????????????
          ??????? ...??????????????????????????????????
          ??? }????????????????????????????????????????
          ??? public boolean overlaps(window w){???????
          ??????? return shape.overlaps(w);??????????? ?
          ??? }????????????????????????????????????????
          ??? public int getArea(){????????????????????
          ??????? return shape.getArea();??????????????
          ??? }????????????????????????????????????????
          }????????????????????????????????????????????
          class引自《The Pragmatic Programmer》)

          posted on 2006-04-06 16:25 蘑菇 閱讀(964) 評論(3)  編輯  收藏 所屬分類: 開發隨感

          評論

          # re: delegation(委托) 2006-04-06 17:34 蘑菇

          加個更容易懂的例子:
          delegateA {
          delegateeB b;

          void methodA() { b.methodB(this); }
          void do() {}
          }

          delegateeB {
          void methodB(delegateA a) { a.do(); }
          }   回復  更多評論   

          # re: delegation(委托) 2006-04-27 15:20 ruidong

          能否解釋一下上述代碼?謝謝蘑菇  回復  更多評論   

          # re: delegation(委托) 2006-04-27 19:14 蘑菇

          A是委托類,B是被委托類。
          A調用B進行委托,委托時A的對象傳遞給了B.
          在B中,使用了A的方法,完成了委托。  回復  更多評論   

          導航

          統計

          公告

          其實生活很簡單

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 武夷山市| 灵川县| 黄山市| 外汇| 台北市| 通海县| 舒城县| 辽阳市| 定边县| 汉沽区| 上虞市| 德兴市| 梁河县| 达日县| 黄山市| 旺苍县| 红河县| 汽车| 伊吾县| 鸡东县| 霍州市| 涟水县| 成安县| 和静县| 陆丰市| 丹棱县| 临沂市| 当涂县| 沧州市| 子洲县| 辰溪县| 嫩江县| 昌吉市| 利川市| 广灵县| 钟山县| 昌江| 东台市| 米林县| 深泽县| 河津市|