zoukankan      html  css  js  c++  java
  • 大话设计模式--桥接模式 Bridge -- C++实现实例

    1. 桥接模式: 将抽象部分与它的实现部分分离,使它们都可以独立的变化。

    分离是指 抽象类和它的派生类用来实现自己的对象分离。

    实现系统可以有多角度分类,每一种分类都有可能变化,那么把这种多角度分离出来让他们独立变化,减少他们之间的耦合。

    实例:

    implementor.h implementor.cpp  实现接口

    #ifndef IMPLEMENTOR_H
    #define IMPLEMENTOR_H
    
    class Implementor
    {
    public:
        Implementor();
        void virtual operation()=0;
    };
    
    #endif // IMPLEMENTOR_H
    #include "implementor.h"
    
    Implementor::Implementor()
    {
    }
    

    concreteimplementora.h concreteimplementora.cpp  具体实现功能A

    #ifndef CONCRETEIMPLEMENTORA_H
    #define CONCRETEIMPLEMENTORA_H
    
    #include "implementor.h"
    
    class ConcreteImplementorA : public Implementor
    {
    public:
        ConcreteImplementorA();
        void operation();
    };
    
    #endif // CONCRETEIMPLEMENTORA_H
    #include "concreteimplementora.h"
    #include <stdio.h>
    
    ConcreteImplementorA::ConcreteImplementorA()
    {
    }
    
    void ConcreteImplementorA::operation()
    {
        printf("ConcreteImplementorA operation
    ");
    }

    concreteimplementorb concreteimplementorb  具体实现功能B

    #ifndef CONCRETEIMPLEMENTORB_H
    #define CONCRETEIMPLEMENTORB_H
    
    #include "implementor.h"
    
    class ConcreteImplementorB : public Implementor
    {
    public:
        ConcreteImplementorB();
        void operation();
    };
    
    #endif // CONCRETEIMPLEMENTORB_H
    #include "concreteimplementorb.h"
    #include <stdio.h>
    
    ConcreteImplementorB::ConcreteImplementorB()
    {
    }
    
    void ConcreteImplementorB::operation()
    {
        printf("ConcreteImplementorB operation
    ");
    }
    


    abstraction.h abstraction.cpp 抽象

    #ifndef ABSTRACTION_H
    #define ABSTRACTION_H
    
    #include "implementor.h"
    
    class Abstraction
    {
    public:
        Abstraction();
        void setImplementor(Implementor *implementor);
        void virtual operation();
    
    protected:
        Implementor *implementor;
    };
    
    #endif // ABSTRACTION_H
    #include "abstraction.h"
    
    Abstraction::Abstraction()
    {
        implementor = 0;
    }
    
    void Abstraction::setImplementor(Implementor *implementor)
    {
        this->implementor = implementor;
    }
    
    void Abstraction::operation()
    {
        implementor->operation();
    }

    refinedabstraction.h refinedabstraction.cpp 精确抽象

    #ifndef REFINEDABSTRACTION_H
    #define REFINEDABSTRACTION_H
    
    #include "abstraction.h"
    
    class RefinedAbstraction : public Abstraction
    {
    public:
        RefinedAbstraction();
        void operation();
    };
    
    #endif // REFINEDABSTRACTION_H
    #include "refinedabstraction.h"
    
    RefinedAbstraction::RefinedAbstraction()
    {
    }
    
    void RefinedAbstraction::operation()
    {
        implementor->operation();
    }

    main.cpp

    #include <iostream>
    #include "refinedabstraction.h"
    #include "concreteimplementora.h"
    #include "concreteimplementorb.h"
    
    using namespace std;
    
    int main()
    {
        cout << "Bridge test!" << endl;
    
        Abstraction *ab = new RefinedAbstraction();
        ab->setImplementor(new ConcreteImplementorA());
        ab->operation();
    
        ab->setImplementor(new ConcreteImplementorB());
        ab->operation();
    
        return 0;
    }




     





     



     

    
  • 相关阅读:
    AppCan打包问题
    Layui数据表格模型
    Layui关闭弹出层并刷新父窗口
    spring boot重启后图片不见
    MySQL数据库创建时间和更新时间错乱问题
    Spring Boot解决无法访问图片的问题
    Spring Boot解决无法访问图片的问题
    reload() 方法用于重新加载当前文档。配合Ajax异步请求。
    Layui数据表格的接口数据请求方式为Get
    时间格式注解
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648181.html
Copyright © 2011-2022 走看看