• 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
  • 相关阅读:
    蓝色光标发布营销行业模型“Blue AI” 人机协同重构产业新格局
    JDK19虚拟线程初探(三)
    【凸优化学习笔记1】什么是优化、优化的数学表达形式
    Linux操作系统——硬盘的挂载和卸载
    Android四大组件之BroadcastReceiver(三)
    磁盘怎么分区?3 款最佳免费磁盘分区软件
    2020秦皇岛(F,E) 思维训练
    【input学习】App对input事件的反馈与waitqueue
    华为交换机镜像端口配置
    算法面试点汇总
  • 原文地址:https://blog.csdn.net/chanlp129/article/details/126111831