代理模式:为其他对象提供一种代理以控制对这个对象的访问;这个应用也非常广泛
按照UML类图,代码如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
class SubjectBase
{
public:
virtual ~SubjectBase()
{
}
virtual void Requst()
{
cout << "SubjectBase::Requst" << endl;
}
};
class ConcreteSubject : public SubjectBase
{
public:
virtual ~ConcreteSubject()
{
}
virtual void Requst()
{
cout << "ConcreteSubject::Requst" << endl;
}
};
class Proxy : public SubjectBase
{
public:
virtual ~Proxy()
{
}
virtual void Requst()
{
cout << "Proxy::Requst" << endl;
m_objConcreteSubject.Requst();
}
private:
ConcreteSubject m_objConcreteSubject;
};
int main()
{
{
Proxy objProxy;
objProxy.Requst();
}
system("pause");
return 0;
}
输出:
Proxy::Requst
ConcreteSubject::Requst