青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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ō),就是一句話(huà):允許將子類(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)用完全依賴(lài)于對(duì)象的真實(shí)類(lèi)型(這就是late binding)。我們的問(wèn)題是:virtual function調(diào)用可不可以依賴(lài)于兩個(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) 閱讀(2232) 評(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è)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产精品二区影院| 午夜老司机精品| 亚洲女爱视频在线| 亚洲欧美另类久久久精品2019| 亚洲精品在线三区| 一个人看的www久久| 亚洲在线不卡| 久久久久国色av免费看影院| 久久一区二区三区四区五区| 欧美激情按摩在线| 亚洲免费成人av| 在线观看日韩精品| 免费影视亚洲| 欧美日韩视频第一区| 国产精品天美传媒入口| 韩国自拍一区| 夜夜嗨av一区二区三区四季av | 久久久久久久综合日本| 久久丁香综合五月国产三级网站| 久久精品国产清自在天天线| 免费看av成人| 在线视频精品一| 久久久国产精品一区二区中文| 免费一级欧美片在线播放| 国产精品福利在线观看| 在线电影一区| 亚洲一区二区在线免费观看| 欧美成人精品福利| 亚洲一区二区三区在线看| 老司机成人网| 国产精品亚发布| 亚洲理论在线观看| 久久夜色精品国产| 中文精品视频| 欧美精品久久99久久在免费线| 国产一区二区在线观看免费播放 | 欧美激情精品久久久久久免费印度| 欧美午夜免费电影| 亚洲国产精品悠悠久久琪琪| 欧美在线视频免费观看| 亚洲人体影院| 毛片一区二区三区| 黄色小说综合网站| 欧美亚洲视频在线观看| 亚洲精品欧洲精品| 欧美成人国产va精品日本一级| 国产亚洲一级| 午夜欧美精品| 亚洲一区中文| 国产精品有限公司| 午夜在线精品| 一区二区三区免费看| 欧美激情一区二区三区在线| 1769国内精品视频在线播放| 久久久777| 亚洲欧美日产图| 国产精品夜夜夜一区二区三区尤| 中文精品99久久国产香蕉| 最新国产成人av网站网址麻豆| 美女视频一区免费观看| 在线免费观看一区二区三区| 久久综合色天天久久综合图片| 性伦欧美刺激片在线观看| 国产欧美一区二区三区久久人妖| 欧美在线一二三| 欧美日韩精品中文字幕| 韩日精品视频一区| 久久―日本道色综合久久| 欧美专区中文字幕| 影音先锋国产精品| 欧美成人r级一区二区三区| 久久亚洲精品伦理| 亚洲精品一二三区| 亚洲理论在线| 国产精品视频yy9099| 亚洲综合社区| 午夜日韩福利| 亚洲二区视频在线| 亚洲国产精品va| 欧美日韩综合精品| 久久av一区二区三区漫画| 久久精品亚洲精品| 亚洲精品乱码| 9i看片成人免费高清| 国产精品一级久久久| 久久久欧美精品| 欧美激情a∨在线视频播放| 亚洲视频欧洲视频| 欧美一区二区三区四区在线| 亚洲国产裸拍裸体视频在线观看乱了中文| 亚洲激情欧美| 国产婷婷97碰碰久久人人蜜臀| 免费欧美在线| 国产精品劲爆视频| 欧美www视频在线观看| 欧美吻胸吃奶大尺度电影| 久久er99精品| 欧美日韩另类在线| 久久综合九色综合久99| 欧美日韩中文在线| 女人天堂亚洲aⅴ在线观看| 国产精品盗摄久久久| 欧美mv日韩mv国产网站app| 欧美视频免费看| 欧美成人a视频| 国产日韩欧美高清免费| 亚洲精品日产精品乱码不卡| 国产日韩欧美制服另类| 亚洲精品乱码久久久久久蜜桃麻豆| 国产精品自在线| 亚洲另类在线一区| 亚洲高清二区| 欧美一区二区黄色| 亚洲女优在线| 欧美日韩一区二区欧美激情| 欧美夫妇交换俱乐部在线观看| 国产精品永久| 一本久道久久综合婷婷鲸鱼| 亚洲国产精品嫩草影院| 久久精品国产99国产精品澳门 | 国产精品国码视频| 亚洲第一福利视频| 尤物精品在线| 久久精品国产一区二区三| 午夜精品亚洲| 国产精品久久久久9999高清| 亚洲日本一区二区| 伊人久久大香线蕉综合热线| 在线观看中文字幕亚洲| 亚洲伊人观看| 亚洲资源av| 欧美日韩一级片在线观看| 91久久在线视频| 亚洲裸体视频| 欧美激情国产日韩精品一区18| 欧美大胆人体视频| 亚洲第一精品夜夜躁人人爽| 久久久久久久国产| 美国三级日本三级久久99| 红桃视频一区| 久久午夜av| 欧美韩国一区| 亚洲精品欧洲精品| 欧美日韩国产不卡在线看| 最新国产の精品合集bt伙计| 亚洲精选视频免费看| 欧美人与禽猛交乱配| 亚洲精品视频啊美女在线直播| 99精品国产高清一区二区| 欧美日韩国产一区二区三区地区| 亚洲精品综合在线| 亚洲欧美视频在线观看视频| 国产日韩精品久久| 久久久噜噜噜久久| 亚洲黄色av一区| 亚洲深夜福利| 国产日韩精品入口| 麻豆久久精品| 一区二区高清| 久久手机免费观看| 亚洲精品视频免费| 国产精品色婷婷久久58| 久久久久久亚洲精品杨幂换脸| 欧美成人dvd在线视频| 一本色道久久综合亚洲精品高清 | 亚洲欧美视频在线观看视频| 国产日韩精品综合网站| 久久一区激情| 在线视频欧美日韩精品| 久久亚洲免费| 亚洲天堂免费在线观看视频| 国产一区再线| 欧美日韩一视频区二区| 久久成人18免费观看| 亚洲精品在线电影| 久久嫩草精品久久久久| 9l国产精品久久久久麻豆| 国产精品视频一区二区高潮| 久久综合狠狠综合久久激情| 一本色道久久加勒比88综合| 欧美成人免费大片| 亚洲制服av| 亚洲欧洲另类| 国产自产精品| 国产精品视频自拍| 欧美高清视频| 久久蜜桃资源一区二区老牛| 亚洲免费不卡| 亚洲国产成人精品视频| 久久精品视频免费| 亚洲一区二区在线免费观看| 亚洲韩日在线| 伊人春色精品| 亚洲第一福利社区| 亚洲桃色在线一区| 一区二区三区在线看| 国产精品久久久久久亚洲毛片| 玖玖综合伊人| 欧美中文字幕不卡| 一区二区三区高清|