代理模式:为其他对象提供一种代理以控制对这个对象的访问。

 

// Proxy.cpp : Defines the entry point for the console application.
//

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


class ISubject
{
public:
 virtual void doSomething() = 0;
};

class ConcreateSubject: public ISubject
{
public:
 virtual void doSomething()
 {
  cout<<"ConcreateSubject doSomething";
 }
};

class Proxy: public ISubject
{
public:
 void setSubject( ISubject *subject )
 {
  mySubject = subject;
 }
 virtual void doSomething()
 {
  if ( mySubject )
   mySubject->doSomething();
 }
 ~Proxy()
 {
  if ( mySubject)
   delete mySubject;
 }

private:
 ISubject *mySubject;
};

int _tmain(int argc, _TCHAR* argv[])
{
 ConcreateSubject * subject = new ConcreateSubject();
 Proxy proxy;
 proxy.setSubject( subject );
 proxy.doSomething();
 system("pause");
 return 0;
}

posted on 2011-12-02 16:26  Just a Programer  阅读(220)  评论(0编辑  收藏  举报