设计模式之类对象结构型 — BRIDGE (桥接)模式

来源:互联网 发布:我的世界商店js 编辑:程序博客网 时间:2024/05/24 03:27

意图

将抽象部分与他的实现部分分离,使他们都可以独立的变化

实现

参考我的这篇文章(点这里),之前已经总结过了,就不重复了。
注意:之前仅仅是对代码依赖性的考虑,这次是在之前的基础上增加了控制不同实现的效果

代码示例

注意:这是完整的示例代码 vs2013编译通过

//windows.h 文件#pragma once#include <iostream>using namespace std;//使用声明式   降低耦合class WindowsImp;class Windows{public:    Windows();    virtual ~Windows();    virtual void paint(){};    virtual void drawContents(){};protected:    WindowsImp *getWindowImp();private:    WindowsImp *m_imp;};class IcoWindows:public Windows{public:    IcoWindows();    ~IcoWindows();    virtual void paint();    virtual void drawContents();private:    //  };class ButtonWindows :public Windows{public:    ButtonWindows();    ~ButtonWindows();    virtual void paint();    virtual void drawContents();private:    //};//windows.cpp 文件#include "Windows.h"#include "WindowsImp.h"#include "WindowsFactory.h"Windows::Windows():m_imp(NULL){}Windows::~Windows(){}WindowsImp * Windows::getWindowImp(){    if (m_imp==NULL)    {        m_imp = WindowsFactory::Instance()->getMicWinImp();    }    return m_imp;}IcoWindows::IcoWindows(){}IcoWindows::~IcoWindows(){}void IcoWindows::paint(){    cout << "我是IcoWindows   paint()" << endl;    //继承至父类,获取WindowsImp的实例    this->getWindowImp()->deviceRect();    return;}void IcoWindows::drawContents(){    cout << "我是IcoWindows   drawContents()" << endl;    this->getWindowImp()->paintBitmap();    return;}ButtonWindows::ButtonWindows(){}ButtonWindows::~ButtonWindows(){}void ButtonWindows::paint(){    cout << "我是ButtonWindows   paint()" << endl;    this->getWindowImp()->deviceRect();    return;}void ButtonWindows::drawContents(){    cout << "我是ButtonWindows   drawContents()" << endl;    this->getWindowImp()->paintBitmap();    return;}----------//windowsImp.h 文件#pragma once#include <iostream>using namespace std;/*Windows 类的实现类 */class WindowsImp{public:    virtual ~WindowsImp();    virtual void paintBitmap() = 0;    virtual void deviceRect() = 0;protected:    WindowsImp();};//微软的版本  class MicrosoftWindowsImp:public WindowsImp{public:    MicrosoftWindowsImp();    ~MicrosoftWindowsImp();    virtual void paintBitmap();    virtual void deviceRect();private:};//苹果系统的版本  class AppleWindowsImp:public WindowsImp{public:    AppleWindowsImp();    ~AppleWindowsImp();    virtual void paintBitmap();    virtual void deviceRect();private:};//windowsImp.cpp 文件#include "WindowsImp.h"WindowsImp::WindowsImp(){}WindowsImp::~WindowsImp(){}MicrosoftWindowsImp::MicrosoftWindowsImp(){}MicrosoftWindowsImp::~MicrosoftWindowsImp(){}void MicrosoftWindowsImp::paintBitmap(){    cout << "微软windows系统的  paintBitmap()" << endl;    return;}void MicrosoftWindowsImp::deviceRect(){    cout << "微软windows系统的  deviceRect()" << endl;}AppleWindowsImp::AppleWindowsImp(){}AppleWindowsImp::~AppleWindowsImp(){}void AppleWindowsImp::paintBitmap(){    cout << "苹果Mac系统的  paintBitmap()" << endl;    return;}void AppleWindowsImp::deviceRect(){    cout << "苹果Mac系统的  deviceRect()" << endl;    return;}----------//WindowsFactory.h文件#pragma once#include <iostream>using namespace std;class WindowsImp;class MicrosoftWindowsImp;class AppleWindowsImp;class WindowsFactory{public:    WindowsFactory();    ~WindowsFactory();     //工厂方法     WindowsImp * getMicWinImp();   //创建微软windows版本的实例    WindowsImp * getAppMacImp();   //创建苹果Mac版本的实例      /* 这里使用了 两个方法  来区分不同的系统版本对应的实现代码 */    /* 也可以通过宏定义 使用一个工厂方法就可以 自动的对应实现的代码 */    /* 也可以在创建Windows类的具体类的实例是 通过传入参数,来控制版本,总之方法很多  */    //单例模式       static WindowsFactory * Instance();private:    static WindowsFactory *m_win;    MicrosoftWindowsImp * m_micWin;    AppleWindowsImp * m_appWin;};//WindowsFactory.cpp文件#include "WindowsFactory.h"#include "WindowsImp.h"WindowsFactory::WindowsFactory():m_micWin(NULL), m_appWin(NULL){}WindowsFactory::~WindowsFactory(){}WindowsImp * WindowsFactory::getMicWinImp(){    if (m_micWin==NULL)    {        m_micWin = new MicrosoftWindowsImp();    }    return m_micWin;}WindowsImp * WindowsFactory::getAppMacImp(){    if (m_appWin==NULL)    {        m_appWin = new AppleWindowsImp();    }    return m_appWin;}WindowsFactory * WindowsFactory::Instance(){    if (m_win == NULL)    {        m_win = new WindowsFactory();    }    return m_win;}WindowsFactory * WindowsFactory::m_win=NULL;----------//main.cpp文件#include "Windows.h"int main(){    IcoWindows ico;    ico.drawContents();    ico.paint();    cout << "\n------------------------\n" << endl;    ButtonWindows butt;    butt.drawContents();    butt.paint();    getchar();    return 0;}

代码解析

  • windows类是 窗口控件的抽象类 IcoWindows 类和 ButtonWIndows 类是他的具体子类,代表两种不同的控件
  • windowsImp 类是 windows类的实现类 他的两个子类分别对应两个不同的实现版本
    使用 windowsImpFactory 类的工厂方法来实例化 windowsImp 的子类
    -windows类中的getWindowImp()方法来返回WindowsImp的实例
    windowsImpFactory 类使用了 单例模式

文件依赖图
这里写图片描述

效果

  • 分离了接口及其实现部分,降低编译的依赖性
  • 提高了可扩充性
  • 隐藏了实现细节

我的个人网站 http://www.breeziness.cn/
我的CSDN http://blog.csdn.net/qq_33775402

转载请注明出处 小风code www.breeziness.cn

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 有鼻毛漏出来了怎么办 被螃蟹夹了肿了怎么办 吃了死了的螃蟹怎么办 被沙漠蝎子咬到怎么办 明台手表掉了怎么办 如果撞了豪车怎么办 车子陷到泥巴里怎么办 乳房一生气就疼怎么办 4s屏幕不显示怎么办 雷神红蜘蛛掉漆怎么办 桔子树根部霉了怎么办 电脑u盘被禁用怎么办 机房u盘限制了怎么办 蜘蛛爬到人身上怎么办 房间里有蛐蛐叫怎么办 养的雨林蝎跑了怎么办 多肉长得太高了怎么办 手被蝎子咬了怎么办 被蝎子蛰住了怎么办 小孩被蝎子蜇了怎么办 孕妇被蝎子蛰了怎么办 怀孕被蝎子蜇了怎么办 孩子被蝎子蛰了怎么办 手被蝎子蛰了怎么办 被东亚钳蝎蛰了怎么办 孕37周羊水过多怎么办 孕38周羊水过多怎么办 孕39周羊水过多怎么办 生完孩子腿关节疼怎么办 手指上长了倒刺怎么办 肥肉吃多了恶心怎么办 大便粘稠怎么回事还便秘怎么办 狗狗大便次数多怎么办 宝宝拉白色稀便怎么办 5个月宝宝流鼻涕怎么办 5个月婴儿流鼻涕怎么办 五个月宝宝流鼻涕鼻塞怎么办 4个月宝宝流鼻涕怎么办 大便经常是稀的怎么办 拉黑色的稀大便怎么办 孕晚期半夜饿了怎么办