Bridge桥接模式2 C++理解的第二个版本

//Bridge桥接模式2 C++理解的第二个版本 
//上一篇是根据我用Delphi实现的Bridge的一个简单改写,
//在网上又看了一些实现,现加入了类的构造与析构函数,
//一边学习设计模式,一边恢复对C++的记忆,不断掌握C++
//下一步将声明与实现分开。

#include
<iostream>

using namespace std;

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

class ConcreteImplementorA: public Implementor
{
public:
    
void OperationImp()
    
{
        cout 
<< "ConcreteImplementorA.OperationImp" << endl;
    }

    ConcreteImplementorA()
{};
    
virtual ~ConcreteImplementorA(){};
}
;

class ConcreteImplementorB: public Implementor
{
public:
    
void OperationImp()
    
{
        cout 
<< "ConcreteImplementorB.OperationImp" << endl;
    }

    ConcreteImplementorB()
{};
    
virtual ~ConcreteImplementorB(){};
}
;


class Abstraction
{
protected:
    Implementor 
*pImp;
    Abstraction()
{};
public:
    
virtual void Operation()
    
{
        
if (pImp)
            pImp
->OperationImp();
    }


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


    
virtual ~Abstraction(){};
}
;

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

    RefinedAbstraction()
{};
    
virtual ~RefinedAbstraction(){};
}
;

int main(int argc, char* argv[])
{
    cout 
<< "hello, 这个一个桥接模式的演示:" << endl; 

    
//RefinedAbstraction* pR = new RefinedAbstraction();
    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;

    
return 0;
}

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