夏天到了

          雨季過(guò)后,是蘑菇生長(zhǎng)的季節(jié)

           

          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 把外界傳來(lái)的訊息" 轉(zhuǎn)送" B ﹐由B 處理之﹐我們稱A 委托B 。當(dāng)一些事物互相溝通分工合作時(shí)常用妥托觀念。

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

          舉個(gè)例子:
          ??? 在某個(gè)框架的組成部分中,對(duì)窗口設(shè)計(jì),可以支持任意形狀的窗口。
          ??? 開(kāi)始的時(shí)候考慮是不是使用子類來(lái)實(shí)現(xiàn),乍一看似乎是比較好的,但是考慮到對(duì)窗口來(lái)說(shuō)形狀只能是它的一項(xiàng)屬性:窗口不是一“種”形狀——窗口“有”形狀。(引自《The Pragmatic Programmer》)所以采用委托的方式。
          ??? 這時(shí),我們定義一個(gè)被委托(delegatee)類Shape和一個(gè)委托(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 蘑菇 閱讀(965) 評(píng)論(3)  編輯  收藏 所屬分類: 開(kāi)發(fā)隨感

          評(píng)論

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

          加個(gè)更容易懂的例子:
          delegateA {
          delegateeB b;

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

          delegateeB {
          void methodB(delegateA a) { a.do(); }
          }   回復(fù)  更多評(píng)論   

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

          能否解釋一下上述代碼?謝謝蘑菇  回復(fù)  更多評(píng)論   

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

          A是委托類,B是被委托類。
          A調(diào)用B進(jìn)行委托,委托時(shí)A的對(duì)象傳遞給了B.
          在B中,使用了A的方法,完成了委托。  回復(fù)  更多評(píng)論   

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          其實(shí)生活很簡(jiǎn)單

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 静海县| 沐川县| 曲阳县| 桦南县| 双鸭山市| 尖扎县| 灵川县| 腾冲县| 如皋市| 平谷区| 巍山| 离岛区| 武安市| 荆门市| 渑池县| 新郑市| 镇平县| 天镇县| 左权县| 班戈县| 治县。| 呼和浩特市| 小金县| 武胜县| 东兰县| 余江县| 安国市| 榕江县| 宜城市| 黄大仙区| 沙坪坝区| 汾西县| 诏安县| 专栏| 梨树县| 新乡县| 阿尔山市| 绵阳市| 丁青县| 汉沽区| 红桥区|