介紹
概念:
Bridge模式又叫做橋接模式,是構造型的設計模式之- -。Bridge模式基於類的最小設計
原則,通過使用封裝,聚合以及繼承等行爲來讓不同的類承擔不同的責任。它的主要特點是
把抽象(abstraction 與行爲實現(implementation)分離開來,從而可以保持各部分的獨
立性以及應對它們的功能擴展。
- Abstractione
抽象類接口(接口或抽象類)維護對行爲實現(Implementor)的引用。 - Refined Abstractione
Abstraction子類。 - Implementors
行爲實現類接口(Abstraction 接口定義了基於Implementor接口的更高層次的操作) - Concretelmplementors
Implementor子類。
優點:將抽象部分與實現部分分離(解耦合),使它們都可以獨立的變化。
UML類圖
簡單代碼:
#ifndef SIMPLE_BRIDGE_H
#define SIMPLE_BRIDGE_H
#include <iostream>
using namespace std;
class Implementor
{
public:
virtual void operation() = 0;
};
class ConcreteImplementorA : public Implementor
{
void operation() override
{
cout<<"具體實現A的方法執行"<<endl;
}
};
class ConcreteImplementorB : public Implementor
{
void operation() override
{
cout<<"具體實現B的方法執行"<<endl;
}
};
class Abstraction
{
public:
void setImplementor(Implementor *imp)
{
m_imp = imp;
}
void operation()
{
m_imp->operation();
}
protected:
Implementor *m_imp;
};
class RefinedAbstraction : public Abstraction
{
public:
void operation()
{
m_imp->operation();
}
};
#endif // SIMPLE_BRIDGE_H
調用:
void simpleBridge()
{
Abstraction *ab = new RefinedAbstraction;
ab->setImplementor( new ConcreteImplementorA);
ab->operation();
ab->setImplementor( new ConcreteImplementorB);
ab->operation();
}
手機軟件和品牌例子:(大話設計模式第22章)
UML圖
代碼:
#ifndef MOBILEPHONE_MODE_H
#define MOBILEPHONE_MODE_H
#include <iostream>
using namespace std;
/**
* @brief The HandsetSoft class
* 手機軟件
*/
class HandsetSoft
{
public:
virtual void run() = 0;
};
/**
* @brief The HandsetGame class
* 手機遊戲
*/
class HandsetGame : public HandsetSoft
{
public:
void run() override
{
cout<<" 運行手機遊戲 "<<endl;
}
};
/**
* @brief The HandsetAddressList class
* 手機通信錄
*/
class HandsetAddressList : public HandsetSoft
{
public:
void run() override
{
cout<<" 運行手機通信錄 "<<endl;
}
};
/**
* @brief The HandsetMedia class
* +新加多媒體
*/
class HandsetMedia : public HandsetSoft
{
public:
void run() override
{
cout<<" 運行手機多媒體 "<<endl;
}
};
/**
* @brief The HandsetBrand class
* 手機品牌
*/
class HandsetBrand
{
public:
//設置手機軟件
void setHandsetSoft(HandsetSoft *soft)
{
m_soft = soft;
}
virtual void run() = 0;
protected:
HandsetSoft *m_soft;
};
/**
* @brief The HandsetBrandN class
* 手機品牌N
*/
class HandsetBrandN : public HandsetBrand
{
public:
void run() override
{
cout<<"手機品牌 N:"<<endl;
m_soft->run();
}
};
/**
* @brief The HandsetBrandM class
* 手機品牌M
*/
class HandsetBrandM : public HandsetBrand
{
public:
void run() override
{
cout<<"手機品牌 M:"<<endl;
m_soft->run();
}
};
/**
* @brief The HandsetBrandS class
* +新增手機品牌
*/
class HandsetBrandS : public HandsetBrand
{
public:
void run() override
{
cout<<"手機品牌 S:"<<endl;
m_soft->run();
}
};
#endif // MOBILEPHONE_MODE_H
調用:
void mobilephoneBidge()
{
HandsetBrand *ab = nullptr;
ab = new HandsetBrandN();
ab->setHandsetSoft( new HandsetGame() );
ab->run();
ab->setHandsetSoft(new HandsetAddressList() );
ab->run();
ab = new HandsetBrandM();
ab->setHandsetSoft( new HandsetGame() );
ab->run();
ab->setHandsetSoft(new HandsetAddressList() );
ab->run();
delete ab;
ab = nullptr;
}