C++ 设计模式 桥接模式(不同手机品牌不同手机软件框架不同软件)
原创
©著作权归作者所有:来自51CTO博客作者mb637c7b0f233f7的原创作品,请联系作者获取转载授权,否则将追究法律责任
文章目录
- 1 理论基础
- 2. 逻辑代码
- 3. 真实应用
1 理论基础
意图:
将抽象部分与实现部分分离,使它们都可以独立的变化。
主要解决:
在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活。
何时使用:
实现系统可能有多个角度分类,每一种角度都可能变化。
如何解决:
把这种多角度分类分离出来,让它们独立变化,减少它们之间耦合。
关键代码:
抽象类依赖实现类。
应用实例:
1、猪八戒从天蓬元帅转世投胎到猪,转世投胎的机制将尘世划分为两个等级,即:灵魂和肉体,前者相当于抽象化,后者相当于实现化。生灵通过功能的委派,调用肉体对象的功能,使得生灵可以动态地选择。
2、墙上的开关,可以看到的开关是抽象的,不用管里面具体怎么实现的。
3、手机软件设计, 不同的手机品牌(iPhone, Google, Microsoft) 对应不同的系统(ios, Android, win) 对应不同的soft(通讯录, 发短信) 下文有具体应用实现
优点:
1、抽象和实现的分离。
2、优秀的扩展能力。
3、实现细节对客户透明。
缺点:
桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。
使用场景:
1、如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。
2、对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。
3、一个类存在两个独立变化的维度,且这两个维度都需要进行扩展。
注意事项:
对于两个独立变化的维度,使用桥接模式再适合不过了。
2. 逻辑代码
#include <iostream>
using namespace std;
// 手机软件抽象类
class Implementor{
public:
virtual ~Implementor(){} // 为了回收ConcreteImplementorA和B
virtual void Operation() = 0;
};
// 手机软件之通讯录
class ConcreteImplementorA : public Implementor{
public:
~ConcreteImplementorA(){
cout << "ConcreteImplementorA 回收了" << endl;
}
virtual void Operation(){
cout << "具体实现 ImplementorA 方法" << endl;
}
};
// 手机软件之游戏
class ConcreteImplementorB : public Implementor{
public:
~ConcreteImplementorB(){
cout << "ConcreteImplementorB 回收了" << endl;
}
virtual void Operation(){
cout << "具体实现 ImplementorB 方法" << endl;
}
};
// 手机品牌大类, 通过设定不同的手机软件架构, 调用对应的游戏和通信录
class Abstraction{
protected:
Implementor *implementor = nullptr;
public:
virtual ~Abstraction(){
if(implementor != nullptr){
delete implementor;
cout << "implementor 已经回收" << endl;
}
} // 为了回收RefinedAbstraction对象
void SetImplementor(Implementor *implementor){
if(this->implementor != nullptr){ //TODO: 关键关键关键
delete this->implementor;
}
this->implementor = implementor;
}
virtual void Operation(){
implementor->Operation();
}
};
// 不同手机品牌调用不同的手机软件架构的不同游戏和通信录
class RefinedAbstraction: public Abstraction{
public:
void Operation(){
implementor->Operation();
}
};
int main(int argc, char const *argv[]){
Abstraction * ab = new RefinedAbstraction();
ab->SetImplementor(new ConcreteImplementorA());
ab->Operation();
// 虽然ab上一个SetImplementor开辟了new 空间, 但是这个函数中有对传入参数之前进行数据回收
ab->SetImplementor(new ConcreteImplementorB());
ab->Operation();
delete ab; // 只需要删除一次即可
return 0;
}
/* 输出:
具体实现 ImplementorA 方法
ConcreteImplementorA 回收了
具体实现 ImplementorB 方法
ConcreteImplementorB 回收了
implementor 已经回收
*/
3. 真实应用
3.1 手机系统
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
// 手机软件类
class HandsetSoft{
public:
virtual ~HandsetSoft(){}
virtual void Run() = 0;
};
// 游戏, 通讯录等具体类
class HandsetGame : public HandsetSoft{
public:
~HandsetGame(){
cout << "HandsetGame 回收" << endl;
}
void Run(){
cout << "运行手机游戏" << endl;
}
};
// 手机通讯录
class HandsetAddressList : public HandsetSoft{
public:
~HandsetAddressList(){
cout << "HandsetAddressList 回收" << endl;
}
void Run(){
cout << "打开通讯录" << endl;
}
};
// 手机品牌
class HandsetBrand{
protected:
HandsetSoft *soft = nullptr;
public:
virtual ~HandsetBrand(){
if(soft != nullptr){
delete soft;
}
cout << "回收开辟的 HandsetSoft" << endl;
}
// 设置手机软件
void SetHandsetSoft(HandsetSoft *soft){
if(this->soft != nullptr){
delete this->soft;
this->soft = nullptr;
}
this->soft = soft;
}
// 运行
virtual void Run()=0;
};
// 具体手机品牌的M具体类
class HanddsetBrand_Google : public HandsetBrand{
public:
void Run(){
soft->Run();
}
};
// 具体手机品牌的N具体类
class HanddsetBrand_Android : public HandsetBrand{
public:
void Run(){
soft->Run();
}
};
int main(int argc, char const *argv[])
{
HandsetBrand *ab = new HanddsetBrand_Google(); // 创建一个手机类
ab->SetHandsetSoft(new HandsetGame()); // 给这个手机的软件加入游戏
ab->Run();
ab->SetHandsetSoft(new HandsetAddressList());
ab->Run();
delete ab;
return 0;
}