• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            sherrylso

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              18 Posts :: 0 Stories :: 124 Comments :: 0 Trackbacks

            1. 什么是Multi-methods.

            在闡述這個概念之前,我們先看一下什么是多態(tài)(Polymorphisn)。多態(tài)是面向?qū)? 象程序設(shè)計的一個重要的特征, 多態(tài)是允許你將父對象的指針(或者引用)設(shè)置成為它的子對象的技術(shù),賦值之后,該父對象指針(或者引用)就可以根據(jù)當前賦 值給它的子對象的特性以不同的方式運作。簡單的說,就是一句話:允許將子類類型的指針賦值給父類類型的指針。多態(tài)性在C++中都是通過虛函數(shù) (Virtual Function) 實現(xiàn)的。 例如:

            class Parent
            ...
            {
                
            public:
                
            virtual void Test() = 0;
            }


            class Sub1: public Parent
            ...
            {
            public:
               
            void Test() 
               ...
            {
                    cout
            <<"HI, this is sub1"<<endl;
               }

            }

            class Sub2: public Parent
            ...
            {
               
            void Test() 
               ...
            {
                    cout
            <<"HI, this is sub2"<<endl;
               }

            }


            Parent
            *  p1 = new Sub1();
            Parent
            *  p2 = new Sub2();
            p1
            ->Test(); //call the Test method of class Sub1;
            p2->Test();//call the Test method of class Sub1;

            在c ++中,多態(tài)性的內(nèi)涵是:通過virtual function技術(shù),真實的函數(shù)調(diào)用完全依賴于對象的真實類型(這就是late binding)。我們的問題是:virtual function調(diào)用可不可以依賴于兩個或者兩個以上的對象?這就是multi-methods.

            例子:

            //method declaration
            bool intersect(const Shape&const Shape&,)
            ...
            {
            //...
            }

            bool intersect(const Rectangle&const Circle&,)
            ...
            {
            //...
            }


            class Shape
            ...
            {
            //...
            }

            class Rectangle: public Shape
            ...
            {
            //...
            }

            class Circle: public Shape
            ...
            {
            //...
            }


            Shape
            * s1 = new Rectangle();
            Shape
            * s2 = new Circle();

            //while calling the following method, what happend?
            //the actual function which will be called is bool intersect(const Shape&, const Shape&,), not bool intersect(const Rectangle&, const Circle&,)
            // but what we want is the latter, not former
            intersect(*s1,*s2);
            //maybe you can call like this, which will meet our requirement, but, unfor?tunately , it violats the principle: we should program based on interface not implementation.
            intersect(*(dynamic_cast<Rectangle*>(s1)),*(dynamic_cast<Circle*>(s2)));



            2. C++ committee曾經(jīng)考慮的解決方案(摘自 The Design and Evolution of C++, by Stroustrup)

            c++從語言自身來支持multi-method:

            //solution 1:
            (s1@s2)->intersect();  //rather than intersect(s1,s2);
            //solution 2:
            bool intersect(virtual const Shape&,virtual const Shape&);
            bool intersect(virtual const Rectangle&,virtual const Circle&//overrides
            {
            }

            或許,C++在將來的某個版本會支持multi-method.

            3. workarounds for multi-method.

            3.1 用double dispatch設(shè)計模式解決。

            double dispatch(雙分派)設(shè)計模式是指:在選擇一個方法的時候,不僅僅要根據(jù)消息接收者(receiver)的運行時型別(Run time type,還要根據(jù)參數(shù)的運行時型別(Run time type。接下來我們用double dispatch來解決一下上面的那個問題:

            class Shape
            ...
            {
            //...
            virtual bool intersect(const Shape&const = 0;
            virtual bool intersect(const Rectangle&const = 0;
            virtual bool intersect(const Circle&const =0;
            }

            class Rectangle: public Shape
            ...
            {
            //...
            bool intersect(const Shape& s) const
            ...
            {
               
            return s.intersect(*this); // *this is a Rectangle and calling which intersect method totally depends on the real type of s!
            }

            bool intersect(const Rectangle&const 
            ...
            {
            //...
            }

            bool intersect(const Circle&const
            ...
            {
            //...
            }

            }

            class Circle: public Shape
            ...
            {
            //...
            bool intersect(const Shape& s) const
            ...
            {
               
            return s.intersect(*this); // *this is a Circle and calling which intersect method totally depends on the real type of s!
            }

            bool intersect(const Rectangle&const 
            ...
            {
            //...
            }

            bool intersect(const Circle&const
            ...
            {
            //...
            }

            }

            對于double dispatch, 最大的問題是:這樣的設(shè)計與類的繼承結(jié)構(gòu)高度耦合,當類的繼承結(jié)構(gòu)改變后,會影響到現(xiàn)有的代碼。從上面的例子可以看到:Shape 類是不應該意識到它的子類的存在。不過當前的主流面向?qū)ο蟪绦蛟O(shè)計語言(C++/Java/C#等)都并不支持multi-method, 從這個意義上說:double dispatch還是一個合理,有效地解決方案

            3.2 另一個可選的方案是RTTI。

            bool intersect(const Shape& s1, const Shape& s2)
            {
            //the follwing code, can implement by using index table, the key is the type_id of the real object(such as typeid(s1) and typeid(s2))
            //the query result is the pointer to function (such as the pointer to function: bool intersect(const Rectangle&, const Circle&).
            if( typeid(s1)==typeid(Rectangle) && typeid(s1)==typeid(Rectangle))
            {
                 intersect(
            *(dynamic_cast<Rectangle*>(s1)),*(dynamic_cast<Circle*>(s2)));
            }

            else if(...)
            {
            //...
            }


            }

             
            bool intersect(const Rectangle&const Circle&,)
            {
            //...
            }


            class Shape
            ......
            {
            //...
            }

            class Rectangle: public Shape
            ......
            {
            //...
            }

            class Circle: public Shape
            ......
            {
            //...
            }


            Shape
            * s1 = new Rectangle();
            Shape
            * s2 = new Circle();
            //as a result , for the caller, the code is based on interface not implementation
            intersect(*s1,*s2);





            posted on 2007-05-06 22:28 愛上龍卷風 閱讀(2210) 評論(4)  編輯 收藏 引用

            Feedback

            # re: C++與double dispatch設(shè)計模式 2009-06-26 18:17 Dan
            很好!明白這個東西了。  回復  更多評論
              

            # re: C++與double dispatch設(shè)計模式[未登錄] 2011-01-08 14:53 darren
            個人感覺還是RTTI比較好,代碼比較簡潔一點。  回復  更多評論
              

            # re: C++與double dispatch設(shè)計模式 2012-02-03 14:52 croma
            這樣的意義何在呢?
            抽象行為的精神不就是簡化程式碼嗎?

            正常的情況下 應該是 Shape 的 class 定義出形狀的描述規(guī)則
            並且在 Intersect 實做這個規(guī)則的計算方法

            子類別只要實作出自己的形狀描述就好了

            我們不在意執(zhí)行階段他是什麼型態(tài),只要他完成 Shape 的描述就好了













              回復  更多評論
              

            # re: C++與double dispatch設(shè)計模式 2013-09-05 01:52 Wangsu

            @croma

            Shape shapes[]={new Circle(),new Rect(), new Something()...};

            foreach(Shape shape in shapes)
            {
            intersect(shape,next shape);<<=這裡會在 Compiler time 就決定調(diào)用 bool intersect(const Shape&, const Shape&,)。
            }
              回復  更多評論
              

            久久中文字幕人妻丝袜| 色综合合久久天天给综看| 一本久久a久久精品vr综合| 欧美大香线蕉线伊人久久| …久久精品99久久香蕉国产| 国产福利电影一区二区三区久久老子无码午夜伦不 | 一本一道久久精品综合| 久久久久久久波多野结衣高潮| 久久精品国产亚洲av麻豆色欲| 久久99精品久久久久久9蜜桃| 国产成人久久精品一区二区三区 | 国产精品狼人久久久久影院| 久久综合亚洲色HEZYO国产| 久久青青草原精品影院| 久久夜色精品国产噜噜麻豆 | 久久人与动人物a级毛片| 久久精品亚洲中文字幕无码麻豆 | 91精品国产高清91久久久久久| 久久AⅤ人妻少妇嫩草影院| 久久这里只有精品18| 亚洲欧洲精品成人久久曰影片 | 亚洲αv久久久噜噜噜噜噜| 亚洲国产成人精品久久久国产成人一区二区三区综 | 国产精品乱码久久久久久软件 | 久久99精品久久只有精品| 久久免费的精品国产V∧| 久久亚洲国产成人影院网站| 久久91精品久久91综合| 精品国产91久久久久久久| 蜜桃麻豆WWW久久囤产精品| 日产久久强奸免费的看| 久久精品二区| 亚洲伊人久久成综合人影院 | 久久久久人妻一区二区三区 | 99久久超碰中文字幕伊人| 伊人久久精品无码二区麻豆| 色婷婷久久综合中文久久一本| 91精品国产91久久久久久蜜臀 | 久久九九青青国产精品| 97久久超碰成人精品网站| 色婷婷综合久久久中文字幕 |