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

sherrylso

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

1. 什么是Multi-methods.

在闡述這個概念之前,我們先看一下什么是多態(tài)(Polymorphisn)。多態(tài)是面向?qū)? 象程序設計的一個重要的特征, 多態(tài)是允許你將父對象的指針(或者引用)設置成為它的子對象的技術(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設計模式解決。

double dispatch(雙分派)設計模式是指:在選擇一個方法的時候,不僅僅要根據(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, 最大的問題是:這樣的設計與類的繼承結(jié)構(gòu)高度耦合,當類的繼承結(jié)構(gòu)改變后,會影響到現(xiàn)有的代碼。從上面的例子可以看到:Shape 類是不應該意識到它的子類的存在。不過當前的主流面向?qū)ο蟪绦蛟O計語言(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 愛上龍卷風 閱讀(2231) 評論(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 定義出形狀的描述規(guī)則
並且在 Intersect 實做這個規(guī)則的計算方法

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

我們不在意執(zhí)行階段他是什麼型態(tài),只要他完成 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 就決定調(diào)用 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>
            久久久亚洲国产天美传媒修理工 | 久久一区免费| 欧美日韩直播| 亚洲无限乱码一二三四麻| 久久午夜精品| 亚洲已满18点击进入久久| 国产一区二区精品在线观看| 美女诱惑一区| 亚洲欧美国产77777| 亚洲黄色一区| 国产精品久久| 亚洲校园激情| 亚洲精品久久久久久久久久久久久| 宅男在线国产精品| 91久久精品国产91久久性色| 国产精品久久久999| 欧美日韩综合网| 国产精品二区影院| 国产精品午夜视频| 黄色国产精品一区二区三区| 国产精品丝袜91| 国产精品伊人日日| 国产麻豆综合| 亚洲国产精品t66y| 亚洲国产一区二区三区青草影视| 久久久噜噜噜久久狠狠50岁| 久久精品成人欧美大片古装| 亚洲欧美日产图| 欧美主播一区二区三区| 久久精视频免费在线久久完整在线看| 亚洲一区二区三区四区中文| 亚洲视频电影图片偷拍一区| 午夜精品久久久久| 久久亚洲综合网| 亚洲精品久久久久中文字幕欢迎你 | 亚洲国产日韩在线| 亚洲另类自拍| 久久香蕉精品| 国产精品一区二区你懂的| 国产精品爱啪在线线免费观看| 国产精品久久久久久久久久ktv| 激情av一区| 久久99在线观看| 99人久久精品视频最新地址| 欧美影院在线播放| 国产精品国产三级国产专播品爱网 | 亚洲美女电影在线| 久久久欧美精品sm网站| 亚洲少妇诱惑| 91久久精品美女高潮| 亚洲一区视频在线| aa日韩免费精品视频一| 99视频精品在线| 欧美中文字幕第一页| 亚洲国产成人porn| 性感少妇一区| 国产精品扒开腿爽爽爽视频 | 国产精品一区二区三区成人| 亚洲精品日韩久久| 玖玖视频精品| 久久久久久夜| 亚洲电影视频在线| 欧美r片在线| 久热精品视频在线| 亚洲高清视频中文字幕| 玖玖视频精品| 狼人天天伊人久久| 91久久精品一区二区三区| 欧美高清在线一区| 欧美多人爱爱视频网站| 日韩视频专区| 一区二区激情小说| 国产精品久久久久永久免费观看| 亚洲天堂av在线免费| 日韩一级精品| 国产精品私人影院| 欧美中文字幕久久| 久久久精品国产99久久精品芒果| 在线观看视频免费一区二区三区| 亚洲国产高清视频| 欧美日韩国产综合视频在线| 中文一区二区| 亚洲男人影院| 国内精品嫩模av私拍在线观看 | 国产精品系列在线播放| 久久国产精品久久w女人spa| 欧美一区午夜视频在线观看| 一区二区视频欧美| 亚洲精品国偷自产在线99热| 国产精品丝袜xxxxxxx| 久久综合九色综合网站 | 国产精品有限公司| 久久色在线播放| 欧美激情麻豆| 久久久久久有精品国产| 欧美成人精品激情在线观看| 亚洲一区精品在线| 久久久久一区二区三区| 亚洲天堂黄色| 久久se精品一区二区| 一区二区精品在线| 久久精品一区二区三区不卡| 欧美一区二区三区四区在线| 欧美成人午夜| 午夜在线观看免费一区| 久久免费观看视频| 亚洲一区二区免费| 久久综合九色综合欧美就去吻| 亚洲天堂成人| 老司机免费视频一区二区| 一本色道久久| 久久综合久久久久88| 久久国产一区二区| 国产精品mv在线观看| 欧美成人网在线| 国产一区二区三区直播精品电影 | 国产一区二区三区久久精品| 亚洲高清资源| 韩日午夜在线资源一区二区| 亚洲美女诱惑| 亚洲国产美女久久久久| 午夜久久美女| 宅男精品导航| 欧美精品九九| 亚洲国产激情| 亚洲国产精品综合| 久久久亚洲精品一区二区三区| 欧美一区二区三区在线视频| 欧美日韩精品欧美日韩精品一| 欧美成人精品影院| 国产中文一区二区| 欧美亚洲三区| 久久精品亚洲国产奇米99| 国产精品家庭影院| 中日韩高清电影网| 午夜精品久久| 国产精品区一区二区三区| 亚洲午夜成aⅴ人片| 亚洲无线视频| 国产精品久久久久7777婷婷| 一本色道久久88亚洲综合88| 在线视频欧美日韩精品| 欧美日韩国产综合在线| 亚洲精品中文字幕在线| 亚洲伦理精品| 欧美日韩精品系列| 亚洲视频大全| 久久成人免费日本黄色| 国产亚洲免费的视频看| 久久精品国产亚洲一区二区三区| 久久久久久久网| 亚洲国产高清高潮精品美女| 久久综合色影院| 亚洲国产精品久久久久秋霞不卡| 亚洲日本电影在线| 欧美日韩影院| 亚洲欧美日韩精品| 久久综合狠狠综合久久综青草 | 国产精品美女久久久免费| 亚洲女优在线| 美女国产一区| 在线亚洲欧美视频| 国产乱码精品一区二区三区忘忧草| 香蕉久久夜色精品| 欧美国产激情| 91久久亚洲| 亚洲国产精品一区二区尤物区| 国产一区自拍视频| 免费成人高清| 亚洲视频在线二区| 久久影音先锋| 亚洲视频999| 一色屋精品视频在线观看网站| 嫩草成人www欧美| 亚洲视频www| 欧美国产欧美亚洲国产日韩mv天天看完整| 亚洲日本成人| 国产欧美日韩一区| 欧美成人中文| 亚洲女人小视频在线观看| 亚洲高清不卡在线观看| 亚洲欧美日韩人成在线播放| 亚洲国产精品99久久久久久久久| 欧美日韩亚洲一区在线观看| 久久精品视频在线播放| 亚洲日本视频| 久久网站免费| 欧美一区二区国产| 一区二区三区欧美亚洲| 亚洲国产精品高清久久久| 国产精品一区=区| 欧美日韩a区| 欧美成熟视频| 久久久久久电影| 午夜欧美精品| 亚洲欧美成人一区二区三区| 亚洲美女电影在线| 亚洲激情在线激情| 欧美黄色一区二区| 噜噜爱69成人精品|