1. 程式人生 > >設計模式之十八:橋接模式(Bridge)

設計模式之十八:橋接模式(Bridge)

ora 它的 pla sin string src ams down ng-

橋接模式:
將抽象部分和它的實現部分相分離開來,以使它們能夠單獨地變化。

UML圖:
技術分享

主要包含:

  1. Abstraction:定義了抽象部分的接口。操作一個實現部分對象的引用。

  2. RefinedAbstraction:繼承自抽象部分的類。
  3. Implementor:實現部分的接口。
  4. ConcreteImplementor:實現了Implementor定義的接口的詳細類。

C++代碼例如以下:

#include <iostream>


using namespace std;

class Implementor
{
    public:
    virtual
void operationImpl()=0; }; class ConcreteImplementorA:public Implementor { public: void operationImpl() { cout<<"ConcreteImplementorA::operationImpl"<<endl; } }; class ConcreteImplementorB:public Implementor { public: void operationImpl() { cout<<"ConcreteImplementorB::operationImpl"
<<endl; } }; class Abstraction { public: virtual void operation()=0; void setImplementor(Implementor * i) { impl=i; } Implementor * getImplementor() { return impl; } protected: Implementor * impl; }; class RefinedAbstraction:public
Abstraction { public: void operation() { impl->operationImpl(); } }; int main() { cout<<"橋接模式樣例"<<endl; Abstraction * ab=new RefinedAbstraction(); Implementor * cia=new ConcreteImplementorA(); ab->setImplementor(cia); ab->operation(); Implementor * cib=new ConcreteImplementorB(); ab->setImplementor(cib); ab->operation(); delete cia; delete cib; delete ab; return 0; }

運行輸出:
技術分享


以下是一個詳細的樣例,看這個詳細的樣例可能好理解一些,摘自大話設計模式:

  1. Abstraction為Phone(手機)。
  2. RefinedAbstraction為Samsung(三星手機)。Huawei(華為手機)。
  3. Implementor為Game(手機遊戲)。
  4. ConcreteImplementor為NeedForSpeed(極品飛車)。QQGame(QQ遊戲),FruitNinjia(水果忍者)。

UML類圖為:
技術分享

C++代碼:

#include <iostream>

using namespace std;

class Game
{
    public:
    virtual void play()=0;
};

class NeedForSpeed :public Game
{
    public:
    virtual void play()
    {
        cout<<"need for speed play"<<endl;
    }
};

class QQGame :public Game
{
    public:
    virtual void play()
    {
        cout<<"QQGame play"<<endl;
    }
};

class FruitNinjia:public Game
{
    public:
    virtual void play()
    {
        cout<<"Fruit Ninjia play"<<endl;
    }
};

class Phone
{
    public:
    virtual void run()=0;
    void setGame(Game *g)
    {   
        game=g;
    }   
    Game * getGame()
    {
        return game;
    }
    protected:
    Game *game;
};


class Samsung:public Phone
{
    public:
    virtual void run()
    {
        cout<<"Samsung :";
        game->play();
    }
};

class HuaWei:public Phone
{
    public:
    virtual void run()
    {
        cout<<"HuaWei :";
        game->play();
    }

};

int main()
{
    cout<<"橋接模式真實的樣例,不同的手機品牌和手機遊戲"<<endl;
    Phone *samsung=new Samsung();
    Phone *huawei=new HuaWei();
    Game * needForSpeed=new NeedForSpeed();
    Game * qqGame=new QQGame();
    Game * fruit=new FruitNinjia();
    samsung->setGame(qqGame);
    samsung->run();
    huawei->setGame(needForSpeed);
    huawei->run();
    samsung->setGame(fruit);
    samsung->run();
    delete samsung;
    delete huawei;
    delete needForSpeed;
    delete qqGame;
    delete fruit;
    return 0;

}

運行輸出:
技術分享

設計模式之十八:橋接模式(Bridge)