代理模式之C++实现


#include  " stdafx.h "
#include <iostream>
#include < string>
#include <map>
using  namespace std;

class ForeignGoods
{
private:
     string name;
public:
    ForeignGoods( string name)
    {
         this->name = name;
    }

     string GetName()
    {
         return  this->name;
    }
};

class Purchase
{
public:
     virtual  void Buy() =  0;
};

class Customer :  public Purchase
{
private:
    ForeignGoods *pGoods;
public:
    Customer(ForeignGoods *goods)
    {
        pGoods = goods;
    }

     void Buy()
    {
        cout <<  " 购买 " << pGoods->GetName() << endl;
    }
};

class Proxy :  public Purchase
{
private:
    Customer *pCustomer;
public:
    Proxy(ForeignGoods *pGoods)
    {
        pCustomer =  new Customer(pGoods);
    }

     void Buy()
    {
        pCustomer->Buy();
    }
};

int main( void)
{
    ForeignGoods *pGoods =  new ForeignGoods( " 香奈儿香水 ");
    Proxy *pProxy =  new Proxy(pGoods);
    pProxy->Buy();
    free(pProxy);
    free(pGoods);
     return  0;

} 

你可能感兴趣的:(代理模式)