設計模式之橋接模式(Bridge)之三(典型結構圖) 結構型模式

來源:互聯網
上載者:User

Bridge是設計模式中比較複雜和難理解的模式之一,也是OO開發和設計中經常會用到的模式之一。使用組合的方式將抽象和實現徹底的解耦,這樣的好處是抽象和實現可以獨立地變化,系統的耦合性也得到了很好的降低。

GOF在說明Bridge模式時,指出"將抽象部分與它的實現部分分離,使得他們可以獨立變化",這句話很簡單,但是也很複雜。原因就在於GOF的那句話中"實現"該怎麼去理解:"實現"特別是和"抽象"放在一起的時候我們預設的理解是"實現"就是"抽象"的具體子類的實現,但是這裡GOF所謂的"實現"的含義不是指抽象基類的具體子類對抽象基類中虛函數(介面)的實現,是和繼承結合在一起的。

而這裡的"實現"的含義指的是怎麼去實現使用者的需求,並且指的是通過組合的方式實現的,因此這裡的實現不是指的繼承基類,實現基類介面,而是指通過對象組合實現使用者的需求,理解了這一點,也就理解了Bridge模式。

實際上使用Brigde模式和使用帶來的問題方式的解決方案的根本區別在於通過繼承還是通過組合的方式去實現一個功能需求,因此物件導向分析和設計中有一個原則就是 Favor Comosition Over Inheritance.

 

Brideg模式典型結構圖

 --------------------------------------Abstraction.h----------------------------------------------------

#ifndef _ABSTRACTION_H_

#define _ABSTRACTION_H_

 

class AbstractionImp;

 

class Abstraction

{

public:

        virtual ~Abstraction();

        virtual void Operation() = 0;

protected:

         Abstraction();

};

 

class RefinedAbstraction:public:Abstraction

{

public:

        RefinedAbstraction(AbstractionImp *imp);

        ~RefinedAbstraction();

        void Operation();

private:

         AbstractionImp *m_imp;

};

 

#endif

 

 

-------------------------------------Abstraction.cpp--------------------------------

#include "Abstraction.h"

#include "AbstractionImp.h"

#include<iostream>

 

using namespace std;

 

Abstraction::Abstraction()

{}

 

Abstraction::~Abstraction()

{}

 

RefinedAbstraction::RefinedAbstraction(AbstractionImp *imp)

{

        m_imp = imp;

}

 

RefinedAbstraction::~RefinedAbstraction()

{}

 

void RefinedAbstraction::Operation()

{

       imp->Operation();

}

 

--------------------------------------AbstractionIMp.h-----------------------------------

#ifndef _ABSTRACTIONIMP_H_

#define _ABSTRACTION_H_

 

class AbstractionImp

{

public:

         virtual ~AbstractionImp();

         virtual void Operaction() = 0;

protected:

          AbstractionImp();

};

 

 

class ConcreateAbstractionImpA:public AbstractionImp

{

 public:

          ConcreateAbstractionImpA();

         ~ConcreateAbstractionImpA();

          virtual void Operation();

};

 

 

class ConcreateAbstractionImpB:public AbstractionImp

{

public:

          ConcreateAbstractionImpB();

         ~ ConcreateAbstractionImpB();

          virtual void Operation();

};

 

 

----------------------------------------------AbstractionImp.cpp-------------------------------

#include "AbstractionImp.h"

#include <iostream>

using namespact std;

 

AbstractionImp::AbstractionImp()

{}

 

AbstractionImp::~AbstractionImp()

{}

 

void AbstractionImp::Operation()

{

         cout <<"AbstractionImp....imp..."<<endl;

 }

 

ConcreateAbstractionImpA::ConcreateAbstractionImpA()

{}

 

ConcreateAbstractionImpB::~ConcreateAbstractionImpA()

{}

 

void ConcreateAbstractionImpA::Operation()

{

        cout<<"ConcreateAbstractionImpA..."<<endl;

}

 

ConcreateAbstractionImpB::ConcreateAbstractionImpB()

{}

 

ConcreateAbstractionImpB::~ConcreateAbstractionImpB()

{}

 

void ConcreateAbstractionImpB::Operation()

{
         cout<<"ConcreateAbstractionImpB..."<<endl;

}

 

-----------------------------------------main.cpp---------------------------------------

#include"Abstraction.h"

#include"AbstractionImp.h"

 

#include<iostream>

using namespact std;

 

int main(int argc,char * argv[])

{

       AbstractionImp * imp = new ConcreateAbstractionImpA();

       Abstraction *abs = new RefinedAbstraction(imp);

       abs->Operation();

       return 0;

 

}

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.