在做面向对象的软件开发时我们往往想达到更高的代码可复用性和更合理的软件颗粒度。
根据《设计模式——可复用面向对象软件的基础》所说:“你必须找到相关的对象,以适当的颗粒度将他们回归类,再定义类的接口和继承层次,建立对象之间的基本关系。你的设计应该对手头的问题有针对性,同时对将来的问题和需求也要有足够的通用性。”
内行的设计者知道:不是解决任何问题都要从头做起。他们更愿意复用以前使用的解决方案。这些重复的模式方案解决特定的问题,使得面向对象的设计更灵活、优雅,最终复用性更好。它们帮助设计者将新的设计建立在以往的工作基础上,复用以往的成功设计方案。一个熟悉这些设计模式的设计者不需要再去发现它们,而能够立即将他们应用于设计问题中。
本系列文章主要参考文献为——设计模式,可复用面向对象软件的基础(Design Patterns Elements of Reusable Object
文章目录
摘要
本章主要说明桥接模式,该设计模式主要意图是:将抽象部分与实现部分分离开来,使它们都可以独立的变化。
如果了解过模板模式的话可以通过与模板模式的比较来学习和理解桥接模式,从某种程度上来说桥接模式可以称作模板模式的进阶版,模板模式是将通用接口虚函数化,通过子类对虚函数接口重载实现,这样就不需要定义大量的函数污染函数空间,也更容易理解。而桥接模式与模板模式的不同在于桥接模式多了一个虚拟的桥接类,通过这个类的继承类来完成具体实现下边我们就通过一个小栗子来说明什么是桥接模式。
主要参与者
该设计模式的参与者有5个,分别是:
- Abstraction 抽象类的接口
- RedefineAbstraction 扩充抽象类的接口
- Implementor 定义实现类的抽象接口
- ConcreteImplementor 具体实现抽象接口的类
- Client 用户
优点
- 分离接口与其实现部分;
- 提高可扩充性,使用更灵活;
- 实现细节对客户透明;(当然你也可以在实现时通过共享或引用机制隐藏这些细节)
具体实现代码
例子中Abstraction 是抽象笔记本电脑,RedefineAbstraction 是不同品牌的笔记本电脑,Implementor 是抽象操作系统,ConcreteImplementor 是具体的操作系统,我们在选择电脑时可以选择不同的电脑品牌,但与此同时我们还需要选择所安装的操作系统,这样我们将买不同电脑品牌和安装不同操作系统的过程分离出来,这大致就是桥接模式的意图,接下来我们通过一个实例代码来说明具体实现,:
抽象类接口(Abstraction )
/****************************************************************
Author : BingLee
Date : 2019-07-18
Info :
https://blog.youkuaiyun.com/Bing_Lee (C)All rights reserved.
******************************************************************/
#ifndef COMPUTER_H
#define COMPUTER_H
#include "OS.h"
class Computer
{
public:
virtual void InstallOS(OS *os) = 0;
};
#endif // COMPUTER_H
扩充抽象类的接口(RedefineAbstraction )
/****************************************************************
Author : BingLee
Date : 2019-07-18
Info :
https://blog.youkuaiyun.com/Bing_Lee (C)All rights reserved.
******************************************************************/
#ifndef DELLCOMPUTER_H
#define DELLCOMPUTER_H
#include "computer.h"
#include "os.h"
class DellComputer : public Computer
{
public:
void InstallOS(OS *os)
{
printf("This is DellComputer");
os->InitOS();
}
};
class AppleComputer : public Computer
{
public:
void InstallOS(OS *os)
{
printf("This is AppleComputer");
os->InitOS();
}
};
#endif // DELLCOMPUTER_H
定义实现类的抽象接口(Implementor )
/****************************************************************
Author : BingLee
Date : 2019-07-18
Info :
https://blog.youkuaiyun.com/Bing_Lee (C)All rights reserved.
******************************************************************/
#ifndef OS_H
#define OS_H
class OS
{
public:
virtual void InitOS() = 0;
};
#endif // OS_H
具体实现抽象接口的类(ConcreteImplementor )
/****************************************************************
Author : BingLee
Date : 2019-07-18
Info :
https://blog.youkuaiyun.com/Bing_Lee (C)All rights reserved.
******************************************************************/
#ifndef CONCRETEOS_H
#define CONCRETEOS_H
#include "os.h"
#include "stdio.h"
class Linux : public OS
{
public:
void InitOS(){printf(" Install Linux.\n");}
};
class Windows : public OS
{
public:
void InitOS(){printf(" Install Windows.\n");}
};
class MacOS : public OS
{
public:
void InitOS(){printf(" Install MacOS.\n");}
};
#endif // CONCRETEOS_H
用户使用(Client)
#include <QCoreApplication>
#include "concreteos.h"
#include "redefinecomputer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//具体操作系统
OS *osLinux = new Linux();
OS *osWindows = new Windows();
OS *osMac = new MacOS();
//具体笔记本电脑
Computer *dellComputer1 = new DellComputer();
Computer *dellComputer2 = new DellComputer();
Computer *appleComputer1 = new AppleComputer();
Computer *appleComputer2 = new AppleComputer();
//安装操作系统
dellComputer1->InstallOS(osLinux);
dellComputer2->InstallOS(osWindows);
appleComputer1->InstallOS(osMac);
appleComputer2->InstallOS(osWindows);
return a.exec();
}
输出结果

补充说明
如果需要专栏所有设计模式的源代码请留言留下你的联系方式,若能点赞、关注就更棒了!!!
本篇博客中的代码均已通过编译,如有Bug,请提出宝贵意见~