• 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
  • 相关阅读:
    [附源码]计算机毕业设计springboot基于VUE的网上订餐系统
    flink1.13.2 text文本数据迁移为orc+snappy数据解决方案
    LeakyReLU激活函数
    隐私计算迎来“开源革命”,高质量项目有哪些?
    Spring-Bean的生命周期
    [附源码]计算机毕业设计JAVA人力资源管理系统论文2022
    云原生Kubernetes:K8S集群各组件服务重启
    CentOS7多种方式安装MySQL-5.7.X数据库
    【YoloDeployCsharp】基于.NET Framework的YOLO深度学习模型部署测试平台
    第3章业务功能开发(实现显示线索主页面,并查询表单各个下拉框数据)
  • 原文地址:https://blog.csdn.net/chanlp129/article/details/126111831