c++ 桥接模式(进阶版)

版权声明:本文为博主原创文章,转载请注明原文地址:http://blog.csdn.net/helloworld_ptt https://blog.csdn.net/helloworld_ptt/article/details/80352841

写本文理由:

加深对设计模式的理解
本文重点在C++实现

桥接模式:

这里写图片描述
[图片来自《设计模式可复用面向对象软件基础》一书(c++)]

Abstraction.hpp

#pragma once                                                                   
#include <iostream>                                                            
#include "AbstractionImpl.hpp"                                                 
class Abstraction                                                              
{                                                                              
    public:                                                                    
        Abstraction(AbstractionImpl* abstractionImpl);                         
        virtual ~Abstraction();                                                
        virtual void operation() = 0;                                          
    protected:                                                                 
        AbstractionImpl* abstractionImpl;           //桥接                           
};                                                                             


class AbstractionA : public Abstraction                                        
{                                                                              
    public:                                                                    
        AbstractionA();                                                                                                                                       
        ~AbstractionA();                                                       
        void operation();                                                      
};                                                                             

class AbstractionB : public Abstraction                                        
{                                                                              
    public:                                                                    
        AbstractionB();                                                        
        ~AbstractionB();                                                       
        void operation();                                                      
};

Abstraction.cpp

#include "Abstraction.hpp"
#include "AbstractionFactory.hpp"
Abstraction::Abstraction(AbstractionImpl* impl)
{
    abstractionImpl = impl;
}

Abstraction::~Abstraction()
{

}

AbstractionA::AbstractionA() :Abstraction(AbstractionFactory::getInstance()->getAbstractionSpecial('A')) 
{
    /**
     *  显示调用Abstraction的构造器
     */
}

AbstractionA::~AbstractionA()
{

}                                             

void AbstractionA::operation()
{
    std::cout<< "In AbstractionA: " ;
    /**
     *  Impl子类(A)的操作
     */
    this->abstractionImpl->operation();
    // Do anything you want to do
}

AbstractionB::AbstractionB() : Abstraction(AbstractionFactory::getInstance()->getAbstractionSpecial('B'))
{
    /**
     *  显示调用Abstraction的构造器
     */
}

AbstractionB::~AbstractionB()
{

}

void AbstractionB::operation()
{
    std::cout<< "In AbstractionB : ";
    /**
     * Impl子类(B)的操作
     */
    this->abstractionImpl->operation();
    // Do anything you want to do
}     

AbstractionImpl.hpp

#pragma once                                                                   
#include <iostream>                                                            
#pragma once                                                                   
#include <iostream>                                                            
class AbstractionImpl                                                          
{                                                                              
public:                                                                        
    AbstractionImpl();                                                         
    virtual ~AbstractionImpl();                                                
    virtual void operation() = 0;                                              
};                                                                             


class AbstractionImplA : public AbstractionImpl                                
{                                                                              
public:                                                                        
    AbstractionImplA();                                                        
    virtual ~AbstractionImplA();                                               
    void operation();                                                          
};                                                                             

class AbstractionImplB : public AbstractionImpl                                
{                                                                              
public:                                                                        
    AbstractionImplB();                                                        
    ~AbstractionImplB();                                                       
    void operation();                                                          
};

AbstractionImpl.cpp

#include "AbstractionImpl.hpp"

AbstractionImpl::AbstractionImpl()
{

}

AbstractionImpl::~AbstractionImpl()
{

}

AbstractionImplA::AbstractionImplA()
{

}

AbstractionImplA::~AbstractionImplA()
{

}

void AbstractionImplA::operation()
{
    std::cout<<"AbstractionImplA"<<std::endl;
}

AbstractionImplB::AbstractionImplB()
{

}

AbstractionImplB::~AbstractionImplB()
{

}
void AbstractionImplB::operation()
{
    std::cout<<"AbstractionImplB"<<std::endl;
}

AbstractionFactory.hpp

#pragma once
#include "AbstractionImpl.hpp"
class AbstractionFactory                                                                                                                                      
{
public:
    static AbstractionFactory* getInstance()
    {
        //加锁
        if(abstractionFactory == nullptr)
        {
            abstractionFactory = new AbstractionFactory; 
        }

        return abstractionFactory;
    }
    AbstractionImpl* getAbstractionSpecial(const char& type)
    {
        switch(type)
        {
            case 'A':
                {
                    return new AbstractionImplA;
                }
                break;
            case 'B':
                {
                    return new AbstractionImplB;
                }
        }
        return NULL;
    }
private:
    static AbstractionFactory* abstractionFactory;
    AbstractionFactory()
    {
    }
};

AbstractionFactory* AbstractionFactory::abstractionFactory = nullptr;

main.cpp

#include "Abstraction.hpp"                                                     
#include <iostream>                                                            

int main()                                                                     
{                                                                              
    Abstraction* abstraction = new AbstractionA();                             
    abstraction->operation();                                                  

    Abstraction* abstraction2 = new AbstractionB();                            
    abstraction2->operation();                                                 

    return 0;                                                                  
}

运行结果:

这里写图片描述

总结:

本文使用了:单例模式-桥接模式
相关模式:抽象工厂模式,适配器模式

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页