C++设计模式4--桥接模式--处理多维度的需求变化

引言

桥接模式就将要解决什么样的问题呢?我们具几个显示中的例子来加深理解

例子一

    拿汽车在路上行驶的来说。即有轿车,又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶。这你会发现,对于不同的交通工具,行驶在不同的环境中,每个都作为一个需求,我们会有很多种可能的需求,

例子二

再比如说我们每天用的电脑,有各种牌子的,什么Dell,Hp,Acer,Lenovo,Apple,不同的电脑上还可以装不一样的操作系统,比如说Windows(WindowsXP,Windows7.Windows8),Linux(RedHat,Ubunto,LinuxMint,CentOS),MacOS等等,如果每台电脑配上一个操作系统,我么也会有很多的组合。

如果我们每个需求都用一个单独的类来对应,很容易出现问题,我们知道在继承的时候基类和子类的耦合度是很高的

传统方式的缺点

①首先它在遵循开放-封闭原则的同时,违背了类的单一职责原则,即一个类只有一个引起它变化的原因,而这里引起变化的原因却有两个,即电脑牌子的变化和操作系统类型的变化;
②其次是重复代码会很多,不同的电脑安装不同的操作系统也会有一部分的代码是相同的;
③再次是类的结构过于复杂,继承关系太多,难于维护,最后最致命的一点是扩展性太差。如果变化沿着电脑的牌子和不同的操作系统两个方向变化,我们会看到这个类的结构会迅速的变庞大。

小结

我们会发现,以前我们需求中的变动都是单维度的,也就是只有一个需求面在变动,而这次我们面临的时,几个不同的方向同时变动,每个变动,都会产生一个新的需求,我们需要去对应这些需求,以前那些模式都是不划算的,这时候桥接模式就出现了,用以应对这种多维度的需求变动

桥接模式概述

在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就要使用Bridge模式。

意图:

GOF在提出桥梁模式的时候指出,桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。这句话有三个关键词,也就是抽象化、实现化和脱耦。


代码我们拿那个电脑上安装操作系统来讲解

首先看操作系统的代码


#ifndef __OS_H_INCLUDED__
#define __OS_H_INCLUDED__


// 操作系统
class OS
{
public :
    virtual ~OS( ){ };
    virtual void InstallOSImage( ) const = 0;          // 安装操作系统

};

// WINDOWS操作系统
class WindowsOS : public OS
{
public:
    virtual ~WindowsOS( ){  };
    void InstallOSImage( ) const
    {
        std::cout <<"安装WINDOWS操作系统" <<std::endl;
    }
};

// LINUX操作系统
class LinuxOS : public OS
{
public:
    virtual ~LinuxOS( ){ };
    void InstallOSImage( ) const
    {
        std::cout <<"安装LINUX操作系统" <<std::endl;
    }
};


// MACOX操作系统
class MacOS : public OS
{
public:
    virtual ~MacOS( ){ };
    void InstallOSImage( ) const
    {
        std::cout <<"安装MACOS操作系统" <<std::endl;
    }
};



#endif // __OS_H_INCLUDED__

下面看电脑的代码

在安装时候,传一个OS*类型的指针进来,从未指定安装某个操作系统

#ifndef __COMPUTER_H_INCLUDED__
#define __COMPUTER_H_INCLUDED__

#include "OS.h"

// 计算机的基类
class Computer
{
public :
    virtual ~Computer( ){ };
    virtual void InstallOS(const OS *os) = 0;          // 在计算机上安装操作系统

};

// 计算机的基类
class DellComputer : public Computer
{
public :
    virtual ~DellComputer( ){ };
    void InstallOS(const OS *os)          // 在计算机上安装操作系统
    {
        std::cout <<"我有台戴尔电脑..." <<std::endl;
        os->InstallOSImage( );                      // 安装操作系统的镜像
        std::cout <<std::endl;
    }
};



// 计算机的基类
class AcerComputer : public Computer
{
public :
    virtual ~AcerComputer( ){ };
    void InstallOS(const OS *os)          // 在计算机上安装操作系统
    {
        std::cout <<"我有台宏基电脑..." <<std::endl;
        os->InstallOSImage( );                      // 安装操作系统的镜像
        std::cout <<std::endl;
    }

};

// 计算机的基类
class AppleComputer : public Computer
{
public :
    virtual ~AppleComputer( ){ };
    void InstallOS(const OS *os)          // 在计算机上安装操作系统
    {
        std::cout <<"我有台苹果电脑..." <<std::endl;
        os->InstallOSImage( );                      // 安装操作系统的镜像
        std::cout <<std::endl;
    }

};
#endif // __COMPUTER_H_INCLUDED__

下面看看主程序该怎么写

#include <iostream>

#include "OS.h"
#include "Computer.h"

using namespace std;

int main()
{
    OS       *os = NULL;
    Computer *com = NULL;

    os = new WindowsOS( );
    com = new DellComputer( );
    com->InstallOS(os);              // 在Dell笔记本上安装Windows操作系统
    delete os;
    os = NULL;
    delete com;
    com = NULL;

    os = new LinuxOS( );
    com = new AcerComputer( );
    com->InstallOS(os);      // 在宏基电脑上安装Linux操作系统
    delete os;
    os = NULL;
    delete com;
    com = NULL;

    os = new MacOS( );
    com = new AppleComputer( );
    com->InstallOS(os); // 在苹果机上安装MacOs
    delete os;
    os = NULL;
    delete com;
    com = NULL;

    return 0;
}

附言-另外的实现方式(组合/聚合)


另外还有一种是采用组合或者聚合的方式去实现代码部解释了直接附上
#include <iostream>

//
// 操作系统
class OS
{
public :
    virtual ~OS( ){ };
    virtual void InstallOSImage( ) const = 0;          // 安装操作系统

};


// WINDOWS操作系统
class WindowsOS : public OS
{
public:
    virtual ~WindowsOS( ){  };
    void InstallOSImage( ) const
    {
        std::cout <<"安装WINDOWS操作系统" <<std::endl;
    }
};

// LINUX操作系统
class LinuxOS : public OS
{
public:
    virtual ~LinuxOS( ){ };
    void InstallOSImage( ) const
    {
        std::cout <<"安装LINUX操作系统" <<std::endl;
    }
};


// MACOX操作系统
class MacOS : public OS
{
public:
    virtual ~MacOS( ){ };
    void InstallOSImage( ) const
    {
        std::cout <<"安装MACOS操作系统" <<std::endl;
    }
};
//
//
//
//
//
// 计算机的基类
class Computer
{
public :
    Computer(OS *os)
    :m_os(os)
    {
    }

    virtual ~Computer( )
    {
    };
    virtual void InstallOS( ) = 0;          // 在计算机上安装操作系统

protected :
    OS  *m_os;                  // 安装的操作系统
};

// 计算机的基类
class DellComputer : public Computer
{
public :
    DellComputer(OS *os)
    :Computer(os)
    {
    }
    virtual ~DellComputer( ){ };
    void InstallOS( )          // 在计算机上安装操作系统
    {
        std::cout <<"This is a DELL computer..." <<std::endl;
        this->m_os->InstallOSImage( );                      // 安装操作系统的镜像
        std::cout <<std::endl;
    }
};



// 计算机的基类
class AcerComputer : public Computer
{
public :
    AcerComputer(OS *os)
    :Computer(os)
    {
    }
    virtual ~AcerComputer( )
    {

    }
    void InstallOS( )          // 在计算机上安装操作系统
    {
        std::cout <<"This is a ACER computer..." <<std::endl;
        this->m_os->InstallOSImage( );                      // 安装操作系统的镜像
        std::cout <<std::endl;
    }

};

// 计算机的基类
class AppleComputer : public Computer
{
public :
    AppleComputer(OS *os)
    :Computer(os)
    {
    }
    virtual ~AppleComputer( ){ };
    void InstallOS( )          // 在计算机上安装操作系统
    {
        std::cout <<"This is a APPLE computer..." <<std::endl;
        this->m_os->InstallOSImage( );                      // 安装操作系统的镜像
        std::cout <<std::endl;
    }

};
//
//
//
//
//
//
int main()
{
    OS       *os = NULL;
    Computer *com = NULL;

    os = new WindowsOS( );
    com = new DellComputer(os);
    com->InstallOS( );// Dell上安装了Windows
    delete os;
    os = NULL;
    delete com;
    com = NULL;

    os = new LinuxOS( );
    com = new AcerComputer(os);
    com->InstallOS( );// 宏基上装了个Linux
    delete os;
    os = NULL;
    delete com;
    com = NULL;

    os = new MacOS( );
    com = new AppleComputer(os);  // 苹果电脑上安装MACOS
    com->InstallOS( );
    delete os;
    os = NULL;
    delete com;
    com = NULL;

    return 0;
}




  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值