• <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++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              18 Posts :: 0 Stories :: 124 Comments :: 0 Trackbacks

            1. 什么是Multi-methods.

            在闡述這個(gè)概念之前,我們先看一下什么是多態(tài)(Polymorphisn)。多態(tài)是面向?qū)? 象程序設(shè)計(jì)的一個(gè)重要的特征, 多態(tài)是允許你將父對(duì)象的指針(或者引用)設(shè)置成為它的子對(duì)象的技術(shù),賦值之后,該父對(duì)象指針(或者引用)就可以根據(jù)當(dāng)前賦 值給它的子對(duì)象的特性以不同的方式運(yùn)作。簡(jiǎn)單的說(shuō),就是一句話:允許將子類(lèi)類(lèi)型的指針賦值給父類(lèi)類(lèi)型的指針。多態(tài)性在C++中都是通過(guò)虛函數(shù) (Virtual Function) 實(shí)現(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)涵是:通過(guò)virtual function技術(shù),真實(shí)的函數(shù)調(diào)用完全依賴于對(duì)象的真實(shí)類(lèi)型(這就是late binding)。我們的問(wèn)題是:virtual function調(diào)用可不可以依賴于兩個(gè)或者兩個(gè)以上的對(duì)象?這就是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++從語(yǔ)言自身來(lái)支持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++在將來(lái)的某個(gè)版本會(huì)支持multi-method.

            3. workarounds for multi-method.

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

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

            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
            ...
            {
            //...
            }

            }

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

            3.2 另一個(gè)可選的方案是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 愛(ài)上龍卷風(fēng) 閱讀(2200) 評(píng)論(4)  編輯 收藏 引用

            Feedback

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

            # re: C++與double dispatch設(shè)計(jì)模式[未登錄](méi) 2011-01-08 14:53 darren
            個(gè)人感覺(jué)還是RTTI比較好,代碼比較簡(jiǎn)潔一點(diǎn)。  回復(fù)  更多評(píng)論
              

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

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

            子類(lèi)別只要實(shí)作出自己的形狀描述就好了

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













              回復(fù)  更多評(píng)論
              

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

            @croma

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

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


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            亚洲色婷婷综合久久| 久久国产精品77777| 99久久精品国产一区二区蜜芽| 亚洲AV日韩精品久久久久久| 伊人久久大香线蕉精品不卡| a级毛片无码兔费真人久久| 97久久精品人妻人人搡人人玩| 亚洲国产精品无码成人片久久| 久久久久亚洲AV无码专区网站| 久久艹国产| 理论片午午伦夜理片久久| 青青草国产97免久久费观看| 欧美日韩成人精品久久久免费看| 久久性生大片免费观看性| 久久精品视屏| 亚洲欧美成人久久综合中文网| 日韩AV毛片精品久久久| 色狠狠久久综合网| 亚洲∧v久久久无码精品| 九九久久自然熟的香蕉图片| 国内精品久久国产大陆| 狠狠久久综合| 久久这里的只有是精品23| 亚洲精品乱码久久久久久自慰| 久久久无码精品亚洲日韩按摩| 99久久精品费精品国产一区二区| 99久久精品国产一区二区三区| 久久久久人妻精品一区三寸蜜桃 | 狠狠色婷婷久久一区二区| 亚洲av日韩精品久久久久久a| 国产一区二区三区久久精品| 久久久久亚洲av成人无码电影| 国内精品久久久久影院老司| 日韩精品久久无码人妻中文字幕 | 一本大道久久东京热无码AV| 久久久久国产精品人妻| 久久99国产精品久久99| 久久婷婷五月综合成人D啪| 无码伊人66久久大杳蕉网站谷歌| 国产精品久久久久天天影视| 久久影视综合亚洲|