• Bridge 模式


    桥接模式的UML结构图:
    在这里插入图片描述
    桥接模式主要包含如下几个角色:

    • Abstraction:抽象类。
    • RefinedAbstraction:扩充抽象类。
    • AbstractionImp:实现类接口。
    • ConcreteAbstractionImp:具体实现类 。
    // Abstraction.h
    
    #pragma once
    
    class AbstractionImp;
    class Abstraction
    {
    public:
    	virtual ~Abstraction();
    	virtual void Operation() = 0;
    protected:
    	Abstraction();
    private:
    };
    class RefinedAbstraction :public Abstraction
    {
    public:
    	RefinedAbstraction(AbstractionImp*
    		imp);
    	~RefinedAbstraction();
    	void Operation();
    protected:
    private:
    	AbstractionImp* _imp;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    //Abstraction.cpp 
    #include "Abstraction.h" 
    #include "AbstractionImp.h" 
    #include  
    using namespace std;
    
    Abstraction::Abstraction()
    {
    	cout << "Abstraction..." << endl;
    }
    
    Abstraction::~Abstraction()
    {
    	cout << "~Abstraction..." << endl;
    }
    
    RefinedAbstraction::RefinedAbstraction(AbstractionImp* imp)
    {
    	cout << "RefinedAbstraction imp..." << endl;
    	_imp = imp;
    }
    
    RefinedAbstraction::~RefinedAbstraction()
    {
    	cout << "~RefinedAbstraction..." << endl;
    }
    
    void RefinedAbstraction::Operation()
    {
    	cout << "RefinedAbstraction Operation..." << endl;
    	_imp->Operation();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    //AbstractionImp.h 
    
    #pragma once
    
    class AbstractionImp
    {
    public:
    	virtual ~AbstractionImp();
    	virtual void Operation() = 0;
    protected:
    	AbstractionImp();
    private:
    };
    class ConcreteAbstractionImpA :public
    	AbstractionImp
    {
    public:
    	ConcreteAbstractionImpA();
    	~ConcreteAbstractionImpA();
    	virtual void Operation();
    protected:
    private:
    };
    class ConcreteAbstractionImpB :public
    	AbstractionImp
    {
    public:
    	ConcreteAbstractionImpB();
    	~ConcreteAbstractionImpB();
    	virtual void Operation();
    protected:
    private:
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    //AbstractionImp.cpp 
    
    #include "AbstractionImp.h" 
    #include  
    using namespace std;
    
    AbstractionImp::AbstractionImp()
    {
    	cout << "AbstractionImp..." << endl;
    }
    
    AbstractionImp::~AbstractionImp()
    {
    	cout << "~AbstractionImp..." << endl;
    }
    
    void AbstractionImp::Operation()
    {
    	cout << "AbstractionImp....Operation..." << endl;
    }
    
    ConcreteAbstractionImpA::ConcreteAbstractionImpA()
    {
    	cout << "ConcreteAbstractionImpA" << endl;
    }
    
    ConcreteAbstractionImpA::~ConcreteAbstractionImpA()
    {
    	cout << "~ConcreteAbstractionImpA" << endl;
    }
    
    void ConcreteAbstractionImpA::Operation()
    {
    	cout << "ConcreteAbstractionImpA....Operation...." << endl;
    }
    
    
    ConcreteAbstractionImpB::ConcreteAbstractionImpB()
    {
    	cout << "ConcreteAbstractionImpB" << endl;
    }
    
    ConcreteAbstractionImpB::~ConcreteAbstractionImpB()
    {
    	cout << "~ConcreteAbstractionImpB" << endl;
    }
    
    void ConcreteAbstractionImpB::Operation()
    {
    	cout << "ConcreteAbstractionImpB....Operation...." << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    // main.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
    //

    #include “Abstraction.h”
    #include “AbstractionImp.h”
    #include
    using namespace std;

    int main()
    {
    AbstractionImp* imp = new ConcreteAbstractionImpA();
    Abstraction* abs = new RefinedAbstraction(imp);
    abs->Operation();

    return 0;
    
    • 1

    }

    执行结果如下:

    AbstractionImp...
    ConcreteAbstractionImpA
    Abstraction...
    RefinedAbstraction imp...
    RefinedAbstraction Operation...
    ConcreteAbstractionImpA....Operation....
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    R语言使用ggpubr包的ggbarplot函数可视化排序条形图(自定义填充色、自定义条形边缘色、自定义调色板、条形图分组排序从小到大、文本标签角度)
    环境响应性介孔二氧化硅复合微球/二氧化硅层状双氢氧化物微球的相关制备
    Termux 使用
    CentOS 7升级gcc/G++版本
    .NET 实现启动时重定向程序运行路径及 Windows 服务运行模式部署
    【Android】-- 四种布局方式(线性布局、相对布局、网格布局、和滚动视图)
    携职教育:人社局通知:取得初级会计证可以申领补贴!至高1500元!
    「学习笔记」Garsia-Wachs 算法
    Go 中的方法
    【Linux篇】之Squid代理服务器配置
  • 原文地址:https://blog.csdn.net/chanlp129/article/details/126111831