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

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 類是不應(yīng)該意識到它的子類的存在。不過當前的主流面向?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 愛上龍卷風 閱讀(2255) 評論(4)  編輯 收藏 引用

Feedback

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

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

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

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

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

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













  回復(fù)  更多評論
  

# 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&,)。
}
  回復(fù)  更多評論
  


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   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>
            91久久精品美女| 久久久久久久性| 国产精品免费观看在线| 一区在线视频| 日韩一区二区电影网| 美国十次了思思久久精品导航| 亚洲欧美中文日韩v在线观看| 欧美日韩国产成人高清视频| 亚洲精品国产视频| 欧美激情第1页| 免费在线亚洲欧美| 亚洲毛片一区| 亚洲午夜高清视频| 国产麻豆视频精品| 久久免费视频这里只有精品| 久久久久九九九| 影音先锋日韩有码| 亚洲欧美日韩一区二区在线 | 国产精品毛片在线| 亚洲精品国产精品国产自| 欧美色另类天堂2015| 亚欧美中日韩视频| 欧美一区二区久久久| 在线精品在线| 亚洲精品一区二区三区福利| 欧美午夜视频在线| 久久噜噜亚洲综合| 国产午夜亚洲精品羞羞网站| 欧美jizz19hd性欧美| 欧美视频精品在线| 中日韩在线视频| 狠狠入ady亚洲精品| 亚洲六月丁香色婷婷综合久久| 亚洲人成在线播放| 在线亚洲一区二区| 亚洲免费在线精品一区| 久久av一区二区| 99精品国产在热久久下载| 模特精品裸拍一区| 亚洲日本一区二区三区| 99精品热视频只有精品10| 欧美电影资源| 久久精品综合一区| 亚洲电影在线免费观看| 亚洲一区二区网站| 亚洲美女啪啪| 欧美系列精品| 性欧美暴力猛交69hd| 亚洲网站视频福利| 亚洲欧美日韩区| 久久久午夜视频| 亚洲国产综合91精品麻豆| 午夜精品久久| 欧美韩国一区| 亚洲图片欧洲图片av| 国产伦精品一区二区三区高清| 久久av资源网| 亚洲区一区二| 久久国产88| 国产精品高潮呻吟久久av黑人| 亚洲国产精品va在线看黑人动漫| 国产精品一级在线| 久久久久久伊人| 亚洲精品日产精品乱码不卡| 欧美一区二区精美| 国产精品福利影院| 久久久免费精品| 一区二区三区产品免费精品久久75 | 亚洲欧美在线免费| 久久一日本道色综合久久| 欧美精品一区二区三区蜜桃 | 免费成人黄色片| 久久久亚洲高清| 亚洲毛片在线观看| 国产日韩欧美综合在线| 欧美a一区二区| 欧美影院视频| 99日韩精品| 亚洲作爱视频| 欧美日韩国产精品一区二区亚洲| 亚洲综合日本| 久久精品首页| 经典三级久久| 国产精品九九| 欧美精品一区二区蜜臀亚洲 | 日韩一二三在线视频播| 日韩一区二区免费高清| 国产日韩av一区二区| 欧美在线免费视频| 一二三区精品| 亚洲精品久久久久久久久久久| 久久欧美肥婆一二区| 午夜免费日韩视频| 亚洲香蕉在线观看| 亚洲精品久久在线| 一色屋精品亚洲香蕉网站| 国产精品男人爽免费视频1| 欧美精品色一区二区三区| 欧美亚洲视频在线观看| 亚洲欧美日产图| 在线视频亚洲欧美| 99国内精品久久| 亚洲第一免费播放区| 亚洲视频二区| 夜夜狂射影院欧美极品| 亚洲精品你懂的| 亚洲国产精品久久久久秋霞不卡 | 国产精品久久久久久久久搜平片 | 欧美成人精品高清在线播放| 欧美中在线观看| 欧美亚洲在线观看| 欧美在线黄色| 久久丁香综合五月国产三级网站| 欧美一区二区视频在线观看| 欧美一区二区三区免费观看视频| 亚洲一区亚洲二区| 一区二区三区在线免费视频| 伊人伊人伊人久久| 亚洲高清视频一区二区| 亚洲欧洲日产国产综合网| 亚洲国产视频一区二区| 亚洲精品视频啊美女在线直播| 最新成人在线| av成人免费| 亚洲一区二区毛片| 香港成人在线视频| 久久久www成人免费精品| 久久精品导航| 欧美福利视频在线| 久久精品99| 欧美成人午夜| 亚洲精品国产系列| 中文有码久久| 午夜一区二区三区在线观看| 久久久久久久成人| 欧美大片在线观看一区| 欧美午夜在线视频| 国产色综合久久| 亚洲黄色性网站| 亚洲一区二区三区在线| 久久久国产精彩视频美女艺术照福利| 欧美成人xxx| 99在线热播精品免费| 午夜免费电影一区在线观看| 久久久水蜜桃| 国产精品美女久久久浪潮软件| 国精品一区二区三区| 国产一区二区三区日韩欧美| 国产麻豆精品theporn| 亚洲国产日韩美| 亚洲在线一区| 欧美国产第二页| 亚洲午夜未删减在线观看| 久久久蜜桃一区二区人| 国产精品成人播放| 亚洲国产欧美精品| 欧美一级免费视频| 亚洲国产精品精华液网站| 亚洲男女自偷自拍图片另类| 免费观看亚洲视频大全| 国产久一道中文一区| 亚洲精品国产精品国自产在线 | 欧美在线免费观看视频| 亚洲成人在线网站| 亚洲电影毛片| 欧美一区在线看| 国产精品www| 亚洲精品日韩综合观看成人91| 久久久噜噜噜久噜久久| 一本色道久久加勒比88综合| 久久婷婷国产麻豆91天堂| 久久久久久国产精品一区| 欧美午夜精品理论片a级大开眼界| 在线欧美日韩国产| 久久九九国产精品| 亚洲一区二区视频在线| 欧美日韩一区二区免费视频| 国产精品综合不卡av| 一本色道88久久加勒比精品 | 99热这里只有成人精品国产| 久色婷婷小香蕉久久| 欧美激情精品久久久六区热门 | 99视频精品| 欧美美女福利视频| 亚洲精品三级| 亚洲福利国产精品| 久久这里有精品视频| 国模精品一区二区三区色天香 | 亚洲尤物在线视频观看| 91久久香蕉国产日韩欧美9色| 久久在线免费观看| 精品成人在线视频| 久久综合五月天婷婷伊人| 欧美激情二区三区| 久久色在线播放| 亚洲国产精品精华液2区45 | 蜜臀av国产精品久久久久| 久久精品国产亚洲a| 欧美区高清在线| av成人免费在线|