桥模式也是设计模式中单一组件模式的一种。什么是单一组件模式呢?
单一组件模式:
如下代码是我们没有学习过桥模式写出的代码,下面代码通过功能分解的方法将一个复杂功能伪代码实现。下面代码挺简单,我就不仔细说明了。
- class Messager {
- public:
- virtual void Login(string username, string password) = 0;
- virtual void SendMessage(string message) = 0;
- virtual void SendPicture(Image image) = 0;
-
- virtual void PlaySound() = 0;
- virtual void DrawShape() = 0;
- virtual void WriteText() = 0;
- virtual void Connect() = 0;
-
- virtual ~Messager(){}
- };
-
- // 平台实现
- class PCMessagerbase :public Messager { //PC平台
- public:
- virtual void PlaySound() {
- //**********
- }
- virtual void PlaySound() {
- //**********
- }
- virtual void WriteText() {
- //**********
- }
- virtual void Connect() {
- //**********
- }
- };
- class MobileMessagerbase :public Messager { //移动平台
- public:
- virtual void PlaySound() {
- //**********
- }
- virtual void PlaySound() {
- //**********
- }
- virtual void WriteText() {
- //**********
- }
- virtual void Connect() {
- //**********
- }
- };
- // 业务抽象
- class PCMessageLite : public PCMessagerbase {
- public:
- virtual void Login(string username, string password) {
- PCMessagerbase::Connect();
- //**********
- }
- virtual void SendMassage(string message) {
- PCMessagerbase::WriteText();
- //*********
- }
- virtual void SendPicture(Image image) {
- PCMessagerbase::DrawShape();
- //*********
- }
- };
- class PCMessagePerfect : public PCMessagerbase {
- public:
- virtual void Login(string username, string password) {
- PCMessagerbase::PlaySound();
- PCMessagerbase::Connect();
- //**********
- }
- virtual void SendMassage(string message) {
- PCMessagerbase::WriteText();
- //*********
- PCMessagerbase::PlaySound();
- }
- virtual void SendPicture(Image image) {
- PCMessagerbase::PlaySound();
- PCMessagerbase::DrawShape();
- //*********
- }
- };
- class MobileMessageLite : public MobileMessagerbase {
- public:
- virtual void Login(string username, string password) {
- MobileMessagerbase::Connect();
- //**********
- }
- virtual void SendMassage(string message) {
- MobileMessagerbase::WriteText();
- //*********
- }
- virtual void SendPicture(Image image) {
- MobileMessagerbase::DrawShape();
- //*********
- }
- };
- class MobileMessagePerfect : public MobileMessagerbase {
- public:
- virtual void Login(string username, string password) {
- MobileMessagerbase::PlaySound();
- MobileMessagerbase::Connect();
- //**********
- }
- virtual void SendMassage(string message) {
- MobileMessagerbase::WriteText();
- //*********
- MobileMessagerbase::PlaySound();
- }
- virtual void SendPicture(Image image) {
- MobileMessagerbase::PlaySound();
- MobileMessagerbase::DrawShape();
- //*********
- }
- };
-
- void process() {
- //编译时装配
- Messager* m = new MobileMessagePerfect();
- }
将上述代码如何修改为桥模式呢?为什么能够修改为桥模式呢?
动机:
从上述子问题拆分方式进行业务代码实现,我们实现了很多类,基类1个,平台实现类2个,业务抽象类2*2个。然而在实际业务中,你的平台实现和业务抽象类将会更多,假设n个实现平台,m个业务抽象方法,那将会有1+n+m*n个类,类的个数随着平台和业务抽象急剧膨胀!
下面代码是修改后代码,用桥模式的实现上述业务。基类Messager分为两个类,一个是Messager类,一个是MessagerImp类,平台的选择通过多态指针messagerImp实现,在运行时被确定。下面的代码将平台变化和业务变化两个维度进行,带动了行为的多态实现,这也就导致未修改前的基类Messager要分为两个类。实现了将抽象部分(业务功能)与实现部分(平台实现)分离,是他们可以独立的变化。
- class Messager { //第一个变化方向,平台变化
- public:
- Messager(MessagerImp* imp):messagerImp(imp){} //初始化多态指针
- MessagerImp* messagerImp; //多态指针决定未来运行时是那个平台
- virtual void Login(string username, string password) = 0;
- virtual void SendMessage(string message) = 0;
- virtual void SendPicture(Image image) = 0;
-
- virtual ~Messager() {}
- };
- class MessagerImp {
- public:
- virtual void PlaySound() = 0;
- virtual void DrawShape() = 0;
- virtual void WriteText() = 0;
- virtual void Connect() = 0;
-
- virtual ~MessagerImp() {};
- };
-
- // 平台实现
- class PCMessagerImp :public MessagerImp { //PC平台
- public:
- virtual void PlaySound() {
- //**********
- }
- virtual void PlaySound() {
- //**********
- }
- virtual void WriteText() {
- //**********
- }
- virtual void Connect() {
- //**********
- }
- };
- class MobileMessagerImp :public MessagerImp { //移动平台
- public:
- virtual void PlaySound() {
- //**********
- }
- virtual void PlaySound() {
- //**********
- }
- virtual void WriteText() {
- //**********
- }
- virtual void Connect() {
- //**********
- }
- };
- // 业务抽象
- //第二个变化,业务的变化(简洁版和完美版)
- class MessageLite:public Messager {
-
- public:
- virtual void Login(string username, string password) {
- messagerImp->Connect();
- //**********
- }
- virtual void SendMassage(string message) {
- messagerImp->WriteText();
- //*********
- }
- virtual void SendPicture(Image image) {
- messagerImp->DrawShape();
- //*********
- }
- };
- class MessagePerfect : public Messager{
- public:
- virtual void Login(string username, string password) {
- messagerImp->PlaySound();
- messagerImp->Connect();
- //**********
- }
- virtual void SendMassage(string message) {
- messagerImp->WriteText();
- //*********
- messagerImp->PlaySound();
- }
- virtual void SendPicture(Image image) {
- messagerImp->PlaySound();
- messagerImp->DrawShape();
- //*********
- }
- };
-
- void process() {
- //运行时装配
- MessagerImp* mImp = new PCMessagerImp();
- Messager* m = new Messager(mImp);
- }
如下图所示是上述代码的桥模式类图
总结: