代理模式
C++
#include <iostream>
using namespace std;class Subject
{
public:virtual void func(){cout << "Subject" << endl;}
};class RealSubject : public Subject
{
public:virtual void func(){cout << "RealSubject" << endl;}
};class Proxy : public Subject
{RealSubject real;public:virtual void func(){cout << "Proxy" << endl;real.func();}
};int main()
{Proxy proxy;proxy.func();return 0;
}
C
#include <stdio.h>typedef void (*FuncPtr)(void);typedef struct
{FuncPtr func;
} Subject;typedef struct
{Subject subject;
} RealSubject;void RealSubject_init(RealSubject *self);
void RealSubject_func(void);typedef struct
{Subject subject;RealSubject real;
} Proxy;void Proxy_init(Proxy *self);
void Proxy_func(Proxy *self);int main()
{Proxy proxy;Proxy_init(&proxy);proxy.subject.func();return 0;
}void RealSubject_init(RealSubject *self)
{self->subject.func = RealSubject_func;
}void RealSubject_func(void)
{printf("RealSubject\n");
}void Proxy_init(Proxy *self)
{RealSubject_init(&self->real);self->subject.func = (FuncPtr)Proxy_func;
}void Proxy_func(Proxy *self)
{printf("Proxy\n");self->real.subject.func();
}