![](http://hi.csdn.net/attachment/201202/12/0_132906266328M1.gif)
#ifndef ABSTRACTION_H
#define ABSTRACTION_H
#include "Implementor.h"
class Abstraction
{
public:
/** Default constructor */
Abstraction();
/** Default destructor */
virtual ~Abstraction();
virtual void Operation();
Implementor* m_pImp;
protected:
private:
};
#endif // ABSTRACTION_H
#include "Abstraction.h"
#include <windows.h>
Abstraction::Abstraction()
{
m_pImp=NULL;
}
Abstraction::~Abstraction()
{
if (NULL!=m_pImp)
{
delete m_pImp;
m_pImp=NULL;
}
}
void Abstraction::Operation()
{
m_pImp->OperationImp();
}
#ifndef ATM_SX_H
#define ATM_SX_H
#include "Abstraction.h"
class ATM_SX : public Abstraction
{
public:
/** Default constructor */
ATM_SX();
/** Default destructor */
virtual ~ATM_SX();
protected:
private:
};
#endif // ATM_SX_H
#include "ATM_SX.h"
ATM_SX::ATM_SX()
{
//ctor
}
ATM_SX::~ATM_SX()
{
//dtor
}
#ifndef IMPLEMENTOR_H
#define IMPLEMENTOR_H
class Implementor
{
public:
/** Default constructor */
Implementor();
/** Default destructor */
virtual ~Implementor();
virtual void OperationImp()=0;
protected:
private:
};
#endif // IMPLEMENTOR_H
#include "Implementor.h"
Implementor::Implementor()
{
//ctor
}
Implementor::~Implementor()
{
//dtor
}
#ifndef IMPLEMENTORA_H
#define IMPLEMENTORA_H
#include "Implementor.h"
class ImplementorA : public Implementor
{
public:
/** Default constructor */
ImplementorA();
/** Default destructor */
virtual ~ImplementorA();
void OperationImp();
protected:
private:
};
#endif // IMPLEMENTORA_H
#include "ImplementorA.h"
#include <iostream>
using namespace std;
ImplementorA::ImplementorA()
{
cout<<"客户A来了"<<endl;
}
ImplementorA::~ImplementorA()
{
cout<<"客户A走了"<<endl;
}
void ImplementorA::OperationImp()
{
cout<<"客户A取钱"<<endl;
}
#ifndef IMPLEMENTORB_H
#define IMPLEMENTORB_H
#include "Implementor.h"
class ImplementorB : public Implementor
{
public:
/** Default constructor */
ImplementorB();
/** Default destructor */
virtual ~ImplementorB();
void OperationImp();
protected:
private:
};
#endif // IMPLEMENTORB_H
#include "ImplementorB.h"
#include <iostream>
using namespace std;
ImplementorB::ImplementorB()
{
cout<<"客户B来了"<<endl;
}
ImplementorB::~ImplementorB()
{
cout<<"客户B走了"<<endl;
}
void ImplementorB::OperationImp()
{
cout<<"客户B取钱"<<endl;
}
#include <iostream>
using namespace std;
#include "ATM_SX.h"
#include "Abstraction.h"
#include "ImplementorA.h"
#include "ImplementorB.h"
int main()
{
Abstraction* m_pATM=new ATM_SX();
m_pATM->m_pImp=new ImplementorA();
m_pATM->Operation();
delete m_pATM;
m_pATM->m_pImp=new ImplementorB();
m_pATM->Operation();
delete m_pATM;
return 0;
}