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

sherrylso

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

1. 什么是Multi-methods.

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

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 ++中,多態性的內涵是:通過virtual function技術,真實的函數調用完全依賴于對象的真實類型(這就是late binding)。我們的問題是:virtual function調用可不可以依賴于兩個或者兩個以上的對象?這就是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曾經考慮的解決方案(摘自 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設計模式解決。

double dispatch(雙分派)設計模式是指:在選擇一個方法的時候,不僅僅要根據消息接收者(receiver)的運行時型別(Run time type,還要根據參數的運行時型別(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, 最大的問題是:這樣的設計與類的繼承結構高度耦合,當類的繼承結構改變后,會影響到現有的代碼。從上面的例子可以看到:Shape 類是不應該意識到它的子類的存在。不過當前的主流面向對象程序設計語言(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 愛上龍卷風 閱讀(2232) 評論(4)  編輯 收藏 引用

Feedback

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

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

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

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

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

我們不在意執行階段他是什麼型態,只要他完成 Shape 的描述就好了













  回復  更多評論
  

# re: C++與double dispatch設計模式 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 就決定調用 bool intersect(const Shape&, const Shape&,)。
}
  回復  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲欧美国产高清va在线播| 亚洲欧美韩国| 欧美日韩成人综合| 欧美sm重口味系列视频在线观看| 久久久久久999| 久久天堂av综合合色| 久久精品国产亚洲一区二区| 久久国产精彩视频| 99精品视频一区| 最新高清无码专区| 一区二区三区波多野结衣在线观看| 亚洲精品视频一区二区三区| 日韩视频免费观看高清完整版| 99国内精品久久| 亚洲欧美日韩在线| 久久久久青草大香线综合精品| 久久夜色精品亚洲噜噜国产mv| 你懂的成人av| 日韩视频第一页| 亚洲欧美另类综合偷拍| 久久亚洲二区| 欧美婷婷久久| 国内精品久久久久久 | 亚洲欧美日韩另类精品一区二区三区| 香蕉久久国产| 亚洲国产精品视频一区| 亚洲精品日本| 亚洲综合另类| 蜜桃av一区二区三区| 日韩视频一区二区在线观看 | 久久久久久欧美| 欧美视频精品一区| 在线观看欧美精品| 亚洲欧美精品一区| 欧美成人免费在线视频| 亚洲一区制服诱惑| 欧美理论在线播放| 在线看欧美视频| 欧美在线亚洲在线| 一本一本大道香蕉久在线精品| 久久久99免费视频| 国产欧美日韩精品专区| 一区二区三区日韩精品| 欧美aaa级| 午夜精品久久久久久| 欧美日韩亚洲国产一区| 伊人色综合久久天天| 久久aⅴ乱码一区二区三区| aaa亚洲精品一二三区| 欧美成人有码| 亚洲黑丝在线| 欧美大片在线影院| 久久亚裔精品欧美| 好看的日韩av电影| 久久精品在这里| 亚洲欧美成人| 国产精品一区二区你懂的| 亚洲在线免费观看| 中文国产成人精品| 国产精品激情| 香蕉av777xxx色综合一区| 一区二区三区高清不卡| 欧美日韩亚洲天堂| 亚洲视频999| 日韩网站在线观看| 欧美视频一区| 亚洲欧美日韩区| 亚洲欧美日韩在线综合| 国产乱码精品一区二区三区av| 亚洲一区在线免费| 亚洲一区美女视频在线观看免费| 欧美系列一区| 欧美一区亚洲| 久久精品久久综合| 亚洲电影一级黄| 亚洲国产精品视频一区| 欧美gay视频| 亚洲视频电影图片偷拍一区| 一本色道**综合亚洲精品蜜桃冫| 国产精品分类| 久久久久成人精品| 乱人伦精品视频在线观看| 亚洲美女尤物影院| 免费成人av在线| 美女黄网久久| 99re6这里只有精品| 日韩天堂在线观看| 国产区精品在线观看| 久久人人97超碰精品888| 久久亚洲一区二区| 在线视频日韩精品| 欧美在线观看一区| 亚洲日本中文字幕| 亚洲一区二区欧美日韩| 国内精品国产成人| 日韩午夜电影在线观看| 国产视频一区欧美| 欧美高潮视频| 国产精品高潮呻吟久久av黑人| 久久久久91| 欧美视频手机在线| 欧美电影免费观看| 国产精品一区二区你懂的| 欧美激情一区| 国产伦理一区| 亚洲欧洲日本国产| 国产揄拍国内精品对白| 亚洲精品乱码久久久久久| 国产日韩欧美不卡在线| 亚洲欧洲日韩综合二区| 国外精品视频| 亚洲一区二区三区视频播放| 91久久黄色| 欧美中文字幕在线播放| 亚洲亚洲精品三区日韩精品在线视频| 久久精品二区亚洲w码| 亚洲已满18点击进入久久| 美女诱惑一区| 久久婷婷色综合| 国产嫩草一区二区三区在线观看| 亚洲国产精品t66y| 在线观看日韩av| 欧美在线free| 久久福利视频导航| 国产精品一区二区在线| 日韩一区二区免费高清| 亚洲日本中文字幕免费在线不卡| 久久成人18免费网站| 欧美一级视频精品观看| 国产精品白丝av嫩草影院| 日韩视频免费大全中文字幕| 99在线精品免费视频九九视| 久久人人97超碰精品888| 欧美色偷偷大香| 亚洲蜜桃精久久久久久久| 亚洲激情精品| 欧美成人免费网| 亚洲国内自拍| 99这里只有精品| 欧美va天堂| 最新成人在线| 一区二区av在线| 欧美日韩福利| 在线亚洲免费视频| 午夜精品一区二区三区四区 | 亚洲私人影院| 午夜免费日韩视频| 久久久亚洲精品一区二区三区| 欧美大片免费| 亚洲黄色影院| 一区二区三区四区五区精品| 欧美激情影院| 亚洲三级影院| 亚洲专区一区| 国产亚洲精久久久久久| 久久久青草婷婷精品综合日韩| 免费欧美在线| 9久re热视频在线精品| 欧美视频一区在线| 午夜视频在线观看一区二区三区 | 日韩午夜精品视频| 欧美日韩一区二区三区在线看| 日韩视频欧美视频| 久久国产精品99精品国产| 精品999在线播放| 欧美精品三区| 亚洲神马久久| 另类综合日韩欧美亚洲| 亚洲免费精彩视频| 国产精品中文字幕欧美| 久久亚洲私人国产精品va| 亚洲精品欧美日韩专区| 欧美一区二区三区四区在线| 在线精品在线| 国产精品久久久久99| 久久久精品一区| 亚洲精品在线观看视频| 久久久久久综合网天天| 99ri日韩精品视频| 国产视频一区二区在线观看| 欧美肥婆bbw| 久久国产精品久久精品国产| 亚洲国产欧美一区二区三区久久 | av不卡在线观看| 国产日韩欧美综合| 欧美大胆成人| 欧美在线播放一区二区| 日韩视频三区| 欧美xart系列高清| 欧美一区二区三区啪啪| 亚洲伦理精品| 狠狠入ady亚洲精品经典电影| 欧美日韩一视频区二区| 乱码第一页成人| 欧美亚洲尤物久久| 亚洲午夜久久久| 亚洲国产精品一区二区第一页| 久久视频一区| 久久精品国语| 欧美一区1区三区3区公司|