Bridge桥接模式3 C++理解的第三个版本 代码更加专业了

将定义与实现分开,分别放到.h和.cpp中;客户端调用也单独实现在一个cpp文件中。

Bridge的头文件uBridge.h
/********************************************************************
    created:    2008/06/04
    filename:   uBridge.h
    author:     张树坤
                
http://zhangsk.cn/

    purpose:    Bridge模式的演示代码

根据我的Delphi模式练习改写
桥接模式cpp实现3,更像cpp一些,声明与实现分开。
重构是根据http://www.cppblog.com/converse/archive/2006/07/23/10376.html进行的。
感谢李创同学通过他的代码我学会了如何将声明放到h中和将实现放到cpp中,^_^
********************************************************************
*/



#ifndef UBRIDGE_H
#define UBRIDGE_H

class Implementor
{
public:
    
virtual void OperationImp() = 0;
    
virtual ~Implementor(){};
protected:
    Implementor()
{};
}
;

class ConcreteImplementorA: public Implementor
{
public:
    ConcreteImplementorA()
{};
    
virtual ~ConcreteImplementorA(){};

    
virtual void OperationImp();
}
;

class ConcreteImplementorB: public Implementor
{
public:
    ConcreteImplementorB()
{};
    
virtual ~ConcreteImplementorB(){};
    
    
virtual void OperationImp();
}
;

class Abstraction
{
protected:
    Implementor 
*pImp;
    Abstraction()
{};
public:
    
virtual ~Abstraction(){};
    
    
virtual void Operation();
    
void SetImplementor(Implementor *p);
}
;

class RefinedAbstraction: public Abstraction
{
public:
    
void Operation();
    RefinedAbstraction()
{};
    
virtual ~RefinedAbstraction(){};
}
;



#endif

桥的实现uBridge.cpp
/********************************************************************
    created:    2008/06/04
    filename:   uBridge.cpp
    author:     张树坤
                
http://zhangsk.cn/

    purpose:    Bridge模式的演示代码
********************************************************************
*/


#include 
"uBridge.h"
#include 
<iostream>

void ConcreteImplementorA::OperationImp()
{
    std::cout 
<< "ConcreteImplementorA.OperationImp\n";
}


void ConcreteImplementorB::OperationImp()
{
    std::cout 
<< "ConcreteImplementorB.OperationImp\n";
}


void Abstraction::Operation()
{
    
if (pImp)
        pImp
->OperationImp();
}


void Abstraction::SetImplementor(Implementor *p)
{
    pImp 
= p;
}


void RefinedAbstraction::Operation()
{
    Abstraction::Operation();
//类似Delphi中的Inherited;
    std::cout << "Do some other thing\n";
}

客户端调用代码unit1.cpp
/********************************************************************
    created:    2008/06/04
    filename:   unit1.cpp
    author:     张树坤
                
http://zhangsk.cn/

    purpose:    Bridge模式 客户端调用代码
********************************************************************
*/


#include 
"uBridge.h"
#include 
<stdlib.h>

int main(int argc, char* argv[])
{
    Abstraction
* pAbstraction = new RefinedAbstraction();
    Implementor 
*pImplementor;

    pImplementor 
= new ConcreteImplementorA();
    pAbstraction
->SetImplementor(pImplementor);
    pAbstraction
->Operation();
    delete pImplementor;

    pImplementor 
= new ConcreteImplementorB();
    pAbstraction
->SetImplementor(pImplementor);
    pAbstraction
->Operation();
    delete pImplementor;

    system(
"pause");
    
return 0;
}

posted @ 2008-06-05 16:37  treemon  阅读(500)  评论(0编辑  收藏  举报