設計模式—-Bridge模式

來源:互聯網
上載者:User

 

原來對bridge模式理解不是很深入,感覺和build模式很相似,今天又看了四人幫的關於bridge模式的描述,有些新的理解

先來說下適用性

1、不想抽象和實現之間有一個固定的綁定關係。(因為程式在運行時實現部分可以被選擇或者切換)。

2、類的抽象以及它的實現都應該可以通過產生子類的方法加以擴充。這時bridge模式使你可以對不同的抽象介面和實現部分進行組合,並分別對他們進行擴充。(常用)

3、對一個抽象的實現部分的修改應對客戶不產生影響,即客戶的代碼不必重新編譯。

4、你想對客戶完全以藏抽象的實現部分。在c++中,類的表示在類介面中是可見的。

5、在多個對象間共用實現(可能是引用計數), 但同時要求客戶並不知道這一點。

還有一種方式沒理解,書上是這麼說的:正如在意圖一節的類圖中所示的那樣,有許多類要產生。這樣一種類階層說明你必須將一個對象分解城兩個部分。這種階層為“嵌套的普化”(nestd generalizations)***

 

結構圖如下

 

範例程式碼如下

#include <QtCore/QCoreApplication>#include <iostream>class Implementor;// 維護一個Implementor類的指標class Abstraction{public:    Abstraction(Implementor* pImplementor);    virtual ~Abstraction();    void Operation();protected:    Implementor* m_pImplementor;};// 為實現Abstraction定義的抽象基類,定義了實現的介面函數class Implementor{public:    Implementor(){}    virtual ~Implementor(){}    virtual void OperationImpl() = 0;};// 繼承自Implementor,是Implementor的不同實現之一class ConcreateImplementorA    : public Implementor{public:    ConcreateImplementorA(){}    virtual ~ConcreateImplementorA(){}    virtual void OperationImpl();};// 繼承自Implementor,是Implementor的不同實現之一class ConcreateImplementorB    : public Implementor{public:    ConcreateImplementorB(){}    virtual ~ConcreateImplementorB(){}    virtual void OperationImpl();};void ConcreateImplementorA::OperationImpl(){    std::cout << "Implementation by ConcreateImplementorA\n";}void ConcreateImplementorB::OperationImpl(){    std::cout << "Implementation by ConcreateImplementorB\n";}Abstraction::Abstraction(Implementor* pImplementor)    : m_pImplementor(pImplementor){}Abstraction::~Abstraction(){}void Abstraction::Operation(){    m_pImplementor->OperationImpl();}template <typename deleteType>inline void deleteObject(const deleteType * obj){    delete obj;    obj = 0;}int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    ConcreateImplementorA *pImplA = new ConcreateImplementorA();    Abstraction *pAbstraction1 = new Abstraction(pImplA);    pAbstraction1->Operation();    ConcreateImplementorB *pImplB = new ConcreateImplementorB();    Abstraction *pAbstraction2 = new Abstraction(pImplB);    pAbstraction2->Operation();    deleteObject(pImplB);    deleteObject(pAbstraction2);    deleteObject(pImplA);    deleteObject(pAbstraction1);    return a.exec();}

 

因為Adapter中包含了一個Adaptee對象,這是一個彙總或者組合的關係。而且也是在Adapter的request方法中調用了Adaptee對象中的方法,從這個角度而言,Adapter模式和Bridge模式是非常類似的。

 

但是,他們之間有本質的區別:

1.       在Adapter模式中,Adaptee本身往往已經是一個具體的、已經存在的類。在Bridge模式中,Implementor則是一個抽象類別或者介面;

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

3.       在Adapter模式中,Adapter類派生於一個抽象類別/介面(客戶程式所期望的)。在Bridge模式中,Abstraction類則不存在這樣的情況。

4.       最本質同時也是最重要的區別是,它們的意圖是不同的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.