代理模式通常找到代理方来管理用户的权限访问。如下图:
cpp
#include<string>
#include<iostream>
using namespace std;
class Video
{
public:
virtual void Free() = 0;
virtual void VIP() = 0;
virtual void Ticket() = 0;
};
class FixbugVideo:public Video
{
public:
virtual void Free() { cout << "free" << endl; }
virtual void VIP() { cout << "VIP" << endl; }
virtual void Ticket() { cout << "Ticket" << endl; }
};
class FreeProxy :public Video
{
private:Video* v;
public:
FreeProxy()
{
v = new FixbugVideo();
}
virtual void Free() { v->Free(); }
virtual void VIP() { cout << "无权限VIP" << endl; }
virtual void Ticket() { cout << "无权限Ticket" << endl; }
};
class VIPProxy :public Video
{
private:Video* v;
public:
VIPProxy()
{
v = new FixbugVideo();
}
virtual void Free() { v->Free(); }
virtual void VIP() { v->VIP(); }
virtual void Ticket() { cout << "无权限Ticket" << endl; }
};
class TicketProxy :public Video
{
private:Video* v;
public:
TicketProxy()
{
v = new FixbugVideo();
}
virtual void Free() { v->Free(); }
virtual void VIP() { v->VIP(); }
virtual void Ticket() { v->Ticket();}
};
void Look(Video* p)
{
p->Free();
p->Ticket();
p->VIP();
}
int main()
{
Video* v = new FreeProxy();
Video* v1 = new VIPProxy();
Video* v3 = new TicketProxy();
Look(v);
Look(v1);
Look(v3);
}