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

積木

No sub title

  C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用鏈接

留言簿(1)

我參與的團(tuán)隊(duì)

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

轉(zhuǎn)載:http://patmusing.blog.163.com/blog/static/13583496020100231174397/


Bridge
模式又稱為Handle/Body模式。

9. C++實(shí)現(xiàn)Structural - Bridge模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

在軟件系統(tǒng)中,經(jīng)常面臨著某些結(jié)構(gòu)復(fù)雜的對(duì)象的創(chuàng)建工作,由于需求的變化,這些對(duì)象經(jīng)常面臨著劇烈的變化,但是他們卻擁有比較穩(wěn)定一致的接口。大部分創(chuàng)建型模式,就是為了解決如何向客戶程序隔離出這些易變對(duì)象,從而使得依賴這些易變對(duì)象的客戶程序不隨著需求的改變而改變。

事實(shí)上,上面的假設(shè)是經(jīng)常面臨劇烈變化的對(duì)象(實(shí)現(xiàn)細(xì)節(jié)b)擁有比較穩(wěn)定一致的接口(抽象B)。現(xiàn)在的問題是,如果抽象B由于一些固有的原因也是面臨著劇烈變化,那應(yīng)該怎么辦?

業(yè)務(wù)舉例:

假如我們需要開發(fā)一個(gè)同時(shí)支持PC和手機(jī)的坦克游戲,游戲在PC和手機(jī)上的功能都一樣,都有同樣的類型,面臨同樣的功能需求變化,坦克有不同的型號(hào):T50T75T90(上面所言的抽象B就是坦克,現(xiàn)在抽象B即坦克本身也要變化了,需要PC上的坦克和手機(jī)上的坦克)

對(duì)于其中的坦克設(shè)計(jì),我們可能很容易設(shè)計(jì)出來一個(gè)Tank的抽象基類,然后各種不同型號(hào)的Tank繼承該基類:

// 抽象的坦克

class Tank

{

public:

virtual void shot() = 0; // 射擊

virtual void run() = 0; // 行進(jìn)

virtual void turn() = 0; // 轉(zhuǎn)向

};

// 不同型號(hào)的坦克

class T50 : public Tank

{

...

};

class T75 : public Tank

{

...

};

class T90 : public Tank

{

...

};

由于PC機(jī)上和手機(jī)的圖形繪制、聲效、操作等具體實(shí)現(xiàn)有很大的差別,因此,對(duì)于各種型號(hào)的坦克,都要提供各種不同平臺(tái)上的實(shí)現(xiàn):

// PC機(jī)上的各型號(hào)坦克的實(shí)現(xiàn)

class PCT50 : public T50

{

...

};

class PCT75 : public T75

{

...

};

class PCT90 : public T90

{

...

};

// 手機(jī)上的各型號(hào)坦克的實(shí)現(xiàn)

class MobileT50 : public T50

{

...

};

class MobileT75 : public T75

{

...

};

class MobileT90 : public T90

{

...

};

這樣以來就有會(huì)產(chǎn)生如上6個(gè)leaf或者terminal 類。

上面的設(shè)計(jì)思路會(huì)帶來很多問題:有很多重復(fù)的代碼,類的結(jié)果過于復(fù)雜,難以維護(hù),以至于引入任何新的平臺(tái),比如TV上的Tank游戲,就會(huì)產(chǎn)生9個(gè)leafterminal類,顯然會(huì)讓整個(gè)類層級(jí)結(jié)構(gòu)劇烈復(fù)雜化起來。

上述問題的結(jié)癥:Tank類具有兩個(gè)變化的維度,即型號(hào)的變化平臺(tái)的變化Bridge設(shè)計(jì)模式就是利用面向?qū)ο蟮募夹g(shù)來使得Tank類型可以輕松地沿著型號(hào)平臺(tái)兩個(gè)方向變化,而不引入額外的復(fù)雜度。

“Decouple an abstraction from its implementation so that the two can vary independently.” – GoF

下面是Bridge設(shè)計(jì)模式的類圖:

9. C++實(shí)現(xiàn)Structural - Bridge模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

另外一個(gè)被廣泛用來說明Bridge設(shè)計(jì)模式的例子,就是GoF原著《Design Patterns, Elements of Reusable Object-Oriented Software》中講到的在不同的圖形系統(tǒng)下繪圖的情況。

下面是C++實(shí)現(xiàn)代碼示例:

// Bridge.h

#include <iostream>

#include <string>

#include <memory>

using namespace std;

class PlatformImplementor;

class Tank

{

protected:

auto_ptr<PlatformImplementor> pli;

public:

Tank(auto_ptr<PlatformImplementor> plim) // plim不能與pli相同,因?yàn)槿绻嗤?span style="font-family: 宋體; color: green; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: Arial; mso-font-kerning: 0pt; mso-no-proof: yes">那么pli = plim;就必須寫成:

{ // this->pli = pli;

pli = plim; // 而這會(huì)導(dǎo)致pli不知所指,這或許算是VS2005的一個(gè)bug

}

virtual string shot() = 0;

virtual string run() = 0;

virtual string turn() = 0;

public:

virtual ~Tank()

{

cout << "in the destructor of Tank..." << endl;

}

};

class PlatformImplementor

{

public:

virtual string draw_tank() = 0;

virtual string move_tank() = 0;

virtual string do_shot() = 0;

virtual string turn() = 0;

public:

virtual ~PlatformImplementor()

{

cout << "in the destructor of PlatformImplementor..." << endl;

}

};

// -------------------------------

class T50 : public Tank

{

public:

T50(auto_ptr<PlatformImplementor> plim) : Tank(plim)

{

string tmp_str = pli->draw_tank();

cout << "T50 - " << tmp_str << endl;

}

string run()

{

return "T50 - " + pli->move_tank();

}

string shot()

{

return "T50 - " + pli->do_shot();

}

string turn()

{

return "T50 - " + pli->turn();

}

~T50()

{

cout << "in the destructor of T50..." << endl;

}

};

class T75 : public Tank

{

public:

T75(auto_ptr<PlatformImplementor> plim) : Tank(plim)

{

string tmp_str = pli->draw_tank();

cout << "T75 - " << tmp_str << endl;

}

string run()

{

return "T75 - " + pli->move_tank();

}

string shot()

{

return "T75 - " + pli->do_shot();

}

string turn()

{

return "T75 - " + pli->turn();

}

~T75()

{

cout << "in the destructor of T75..." << endl;

}

};

class T90 : public Tank

{

public:

T90(auto_ptr<PlatformImplementor> plim) : Tank(plim)

{

string tmp_str = pli->draw_tank();

cout << "T90 - " << tmp_str << endl;

}

string run()

{

return "T90 - " + pli->move_tank();

}

string shot()

{

return "T90 - " + pli->do_shot();

}

string turn()

{

return "T90 - " + pli->turn();

}

~T90()

{

cout << "in the destructor of T90..." << endl;

}

};

// --------------------------

class PCPlatformImplementor : public PlatformImplementor

{

public:

string draw_tank()

{

return "PC platform: Draw a tank"; // 假定在這里畫坦克

}

string move_tank()

{

return "PC platform: Move a tank"; // 假定在這里移動(dòng)坦克

}

string do_shot()

{

return "PC platform: Fire the target"; // 假定在這里坦克開火

}

string turn()

{

return "PC platform: Turn direction"; // 假定在這里坦克轉(zhuǎn)彎

}

public:

~PCPlatformImplementor()

{

cout << "in the destructor of PCPlatformImplementor..." << endl;

}

};

class MobilePlatformImplementor : public PlatformImplementor

{

public:

string draw_tank()

{

return "Mobile platform: Draw a tank";

}

string move_tank()

{

return "Mobile platform: Move a tank";

}

string do_shot()

{

return "Mobile platform: Fire the target";

}

string turn()

{

return "Mobile platform: Turn direction";

}

public:

~MobilePlatformImplementor()

{

cout << "in the destructor of MobilePlatformImplementor..." << endl;

}

};

// 測試代碼:Bridge.cpp

#include "Bridge.h"

int main(int argc, char **argv)

{

auto_ptr<PlatformImplementor> pc_pli1(new PCPlatformImplementor);

T50 *pc_T50 = new T50(pc_pli1);

cout << pc_T50->turn() << endl;

cout << pc_T50->shot() << endl;

cout << pc_T50->turn() << endl;

// 由于auto_ptr的特性,pc_pli1到此已經(jīng)無所指向,詳見Tank類的構(gòu)造函數(shù)

delete pc_T50;

cout << "--------------------------------------" << endl;

auto_ptr<PlatformImplementor> pc_pli2(new PCPlatformImplementor);

T75 *pc_T75 = new T75(pc_pli2);

cout << pc_T75->turn() << endl;

cout << pc_T75->shot() << endl;

cout << pc_T75->turn() << endl;

// 由于auto_ptr的特性,pc_pli2到此已經(jīng)無所指向,詳見Tank類的構(gòu)造函數(shù)

delete pc_T75;

cout << "--------------------------------------" << endl;

auto_ptr<PlatformImplementor> pc_pli3(new PCPlatformImplementor);

T90 *pc_T90 = new T90(pc_pli3);

cout << pc_T90->turn() << endl;

cout << pc_T90->shot() << endl;

cout << pc_T90->turn() << endl;

// 由于auto_ptr的特性,pc_pli3到此已經(jīng)無所指向,詳見Tank類的構(gòu)造函數(shù)

delete pc_T90;

cout << "--------------------------------------" << endl;

auto_ptr<PlatformImplementor> mo_pli1(new MobilePlatformImplementor);

T50 *mo_T50 = new T50(mo_pli1);

cout << mo_T50->turn() << endl;

cout << mo_T50->shot() << endl;

cout << mo_T50->turn() << endl;

// 由于auto_ptr的特性,mo_pli1到此已經(jīng)無所指向

delete mo_T50;

cout << "--------------------------------------" << endl;

auto_ptr<PlatformImplementor> mo_pli2(new MobilePlatformImplementor);

T75 *mo_T75 = new T75(mo_pli2);

cout << mo_T75->turn() << endl;

cout << mo_T75->shot() << endl;

cout << mo_T75->turn() << endl;

// 由于auto_ptr的特性,mo_pli2到此已經(jīng)無所指向

delete mo_T75;

cout << "--------------------------------------" << endl;

auto_ptr<PlatformImplementor> mo_pli3(new MobilePlatformImplementor);

T90 *mo_T90 = new T90(mo_pli3);

cout << mo_T90->turn() << endl;

cout << mo_T90->shot() << endl;

cout << mo_T90->turn() << endl;

// 由于auto_ptr的特性,mo_pli3到此已經(jīng)無所指向,詳見Tank類的構(gòu)造函數(shù)

delete mo_T90;

return 0;

}

運(yùn)行結(jié)果:

T50 - PC platform: Draw a tank

T50 - PC platform: Turn direction

T50 - PC platform: Fire the target

T50 - PC platform: Turn direction

in the destructor of T50...

in the destructor of Tank...

in the destructor of PCPlatformImplementor...

in the destructor of PlatformImplementor...

--------------------------------------

T75 - PC platform: Draw a tank

T75 - PC platform: Turn direction

T75 - PC platform: Fire the target

T75 - PC platform: Turn direction

in the destructor of T75...

in the destructor of Tank...

in the destructor of PCPlatformImplementor...

in the destructor of PlatformImplementor...

--------------------------------------

T90 - PC platform: Draw a tank

T90 - PC platform: Turn direction

T90 - PC platform: Fire the target

T90 - PC platform: Turn direction

in the destructor of T90...

in the destructor of Tank...

in the destructor of PCPlatformImplementor...

in the destructor of PlatformImplementor...

--------------------------------------

T50 - Mobile platform: Draw a tank

T50 - Mobile platform: Turn direction

T50 - Mobile platform: Fire the target

T50 - Mobile platform: Turn direction

in the destructor of T50...

in the destructor of Tank...

in the destructor of MobilePlatformImplementor...

in the destructor of PlatformImplementor...

--------------------------------------

T75 - Mobile platform: Draw a tank

T75 - Mobile platform: Turn direction

T75 - Mobile platform: Fire the target

T75 - Mobile platform: Turn direction

in the destructor of T75...

in the destructor of Tank...

in the destructor of MobilePlatformImplementor...

in the destructor of PlatformImplementor...

--------------------------------------

T90 - Mobile platform: Draw a tank

T90 - Mobile platform: Turn direction

T90 - Mobile platform: Fire the target

T90 - Mobile platform: Turn direction

in the destructor of T90...

in the destructor of Tank...

in the destructor of MobilePlatformImplementor...

in the destructor of PlatformImplementor...

上述實(shí)現(xiàn)代碼中各個(gè)類和Bridge模式中的各個(gè)類之間的對(duì)應(yīng)關(guān)系:

Tank < ------ > Abstraction

T50T75T90 < ------ > RefinedAbstraction

PlatformImplementor < ------ > Implementor

PCPlatformImplementor < ------ > ConcreteImplementorA

MobilePlatformImplementor < ------ > ConcreteImplementorB

前面講到的Adapter模式(對(duì)象適配器形式),從UML的角度來看,也可以畫成:

9. C++實(shí)現(xiàn)Structural - Bridge模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

因?yàn)?/span>Adapter中包含了一個(gè)Adaptee對(duì)象,這是一個(gè)聚合或者組合的關(guān)系。而且也是在Adapterrequest方法中調(diào)用了Adaptee對(duì)象中的方法,從這個(gè)角度而言,Adapter模式和Bridge模式是非常類似的。

但是,他們之間有本質(zhì)的區(qū)別:

1. Adapter模式中,Adaptee本身往往已經(jīng)是一個(gè)具體的、已經(jīng)存在的類。在Bridge模式中,Implementor則是一個(gè)抽象類或者接口;

2. Adapter模式中,Adapter類也是一個(gè)具體的類。在Bridge模式中,Abstraction則是一個(gè)抽象類;

3. Adapter模式中,Adapter類派生于一個(gè)抽象類/接口(客戶程序所期望的)。在Bridge模式中,Abstraction類則不存在這樣的情況。

4. 最本質(zhì)同時(shí)也是最重要的區(qū)別是,它們的意圖是不同的。



posted on 2013-03-07 23:09 Jacc.Kim 閱讀(304) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 設(shè)計(jì)模式
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产在线观看精品一区二区三区| 亚洲三级免费| 亚洲成人在线视频网站| 亚洲人成亚洲人成在线观看图片| 一区二区三区精品国产| 久久精品国产2020观看福利| 欧美.www| 亚洲私人黄色宅男| 老鸭窝91久久精品色噜噜导演| 欧美喷水视频| 国产一区二区久久久| 亚洲韩国日本中文字幕| 亚洲女同精品视频| 女同一区二区| 亚洲欧美怡红院| 欧美国产第一页| 国产亚洲精品久久久久婷婷瑜伽| 亚洲激情小视频| 久久国产88| 99视频超级精品| 久久综合色天天久久综合图片| 国产精品h在线观看| 在线观看亚洲一区| 亚洲欧美亚洲| 亚洲人成在线观看一区二区| 亚洲欧美日韩直播| 欧美美女bbbb| 亚洲成色999久久网站| 亚洲欧美国产va在线影院| 欧美激情二区三区| 久久精品国产精品亚洲| 欧美视频手机在线| 91久久精品国产91性色| 久久精品一区| 一区二区三区四区国产| 免费中文日韩| 极品尤物一区二区三区| 性8sex亚洲区入口| 亚洲乱码久久| 欧美顶级大胆免费视频| 韩日成人av| 欧美一区二区视频97| 99综合在线| 欧美精品亚洲一区二区在线播放| 1024国产精品| 久久精品欧美日韩精品| 亚洲私人影院在线观看| 欧美日韩高清不卡| 亚洲精品久久久久久下一站 | 亚洲一区日本| 欧美日韩视频一区二区| 亚洲三级影片| 欧美承认网站| 久久久噜噜噜久久中文字幕色伊伊| 国产精品一区久久久| 亚洲欧美不卡| 99这里有精品| 欧美日韩亚洲一区三区 | 一区二区国产在线观看| 亚洲福利视频三区| 裸体一区二区| 伊人久久噜噜噜躁狠狠躁 | 欧美大色视频| 最近看过的日韩成人| 欧美激情91| 免费影视亚洲| 亚洲精品一区在线| 亚洲国产三级在线| 欧美激情精品久久久久久久变态| 亚洲国产一区二区三区高清| 欧美高清一区| 欧美高清成人| 艳女tv在线观看国产一区| 最新国产成人av网站网址麻豆| 蜜臀av性久久久久蜜臀aⅴ| 亚洲欧洲另类| 亚洲免费av观看| 欧美色123| 午夜精品视频在线| 午夜久久一区| 韩日成人在线| 欧美激情网友自拍| 欧美日韩成人精品| 亚洲午夜精品一区二区| 一区二区三区四区国产精品| 国产精品视频专区| 久久久久久亚洲精品中文字幕| 欧美在线视频免费| 亚洲电影免费在线| 亚洲国产欧美久久| 欧美日韩在线观看一区二区| 性久久久久久久久久久久| 香蕉视频成人在线观看| 一区二区三区在线免费视频| 亚洲国产精品一区制服丝袜| 欧美日韩国产一区精品一区| 小黄鸭精品aⅴ导航网站入口| 香蕉av777xxx色综合一区| 狠狠色综合网| 亚洲激情午夜| 国产精品国产三级国产专播品爱网| 欧美伊人精品成人久久综合97| 久久九九99| 日韩亚洲精品电影| 亚洲一区二区av电影| 黑人中文字幕一区二区三区| 欧美激情一区二区三区| 欧美性开放视频| 久久九九热re6这里有精品| 久久女同精品一区二区| 亚洲无毛电影| 久久精品国亚洲| 一本不卡影院| 欧美一区二区三区精品 | 亚洲麻豆av| 国产午夜亚洲精品不卡| 亚洲电影免费观看高清| 国产精品毛片在线| 欧美成人一区二免费视频软件| 欧美色图麻豆| 女生裸体视频一区二区三区| 欧美涩涩网站| 欧美a级片网站| 国产精品video| 欧美激情精品| 国产亚洲二区| 日韩亚洲在线| 韩国女主播一区二区三区| 亚洲精品一区二区三| 国内外成人在线视频| 日韩视频二区| 亚洲东热激情| 亚欧成人在线| 亚洲私拍自拍| 麻豆freexxxx性91精品| 香蕉久久国产| 欧美日本国产精品| 免费成人av在线看| 国产精品一二三四区| 亚洲人久久久| 在线成人av.com| 亚洲欧美日韩在线高清直播| 99在线热播精品免费| 久久亚洲影音av资源网| 欧美中文在线视频| 欧美日韩亚洲91| 亚洲第一天堂无码专区| 国内精品写真在线观看| 中文亚洲字幕| 99综合在线| 美腿丝袜亚洲色图| 麻豆精品91| 国产区精品视频| 在线一区二区三区四区| 一区二区三区精品在线| 免费在线视频一区| 欧美成人免费全部| 好吊妞这里只有精品| 亚洲女人av| 午夜在线一区二区| 欧美亚州韩日在线看免费版国语版| 亚洲丰满少妇videoshd| 亚洲国产成人精品视频| 久久国产婷婷国产香蕉| 欧美中文在线免费| 国产美女精品在线| 亚洲视频一区在线观看| 亚洲视频视频在线| 欧美日韩天天操| 亚洲乱码国产乱码精品精可以看| 亚洲理论在线| 欧美了一区在线观看| 亚洲国产美女精品久久久久∴| 亚洲国产精品一区二区www| 久久久伊人欧美| 麻豆精品视频在线| 在线成人激情黄色| 久久深夜福利免费观看| 美女成人午夜| 一区二区在线看| 麻豆精品一区二区综合av| 免费在线播放第一区高清av| 一区在线观看视频| 久久久久国色av免费看影院 | 日韩亚洲视频在线| 欧美日韩福利在线观看| 99精品视频网| 亚洲影音一区| 国产日韩欧美一区二区三区四区| 亚洲在线播放电影| 久久国产精品99精品国产| 国产欧美视频一区二区三区| 午夜精彩国产免费不卡不顿大片| 久久精品视频在线观看| **欧美日韩vr在线| 欧美成人精品不卡视频在线观看| 亚洲国产天堂久久综合网| avtt综合网| 国产精品亚洲欧美| 欧美综合77777色婷婷|