本文共 1309 字,大约阅读时间需要 4 分钟。
1 、抽象类 Subject |
public abstract class Subject { public abstract void Request(); } |
2 、真实实体 RealSubject 继承于 Subject |
public class RealSubject :Subject { public override void Request() { Console .WriteLine(" 对象的真实实体方法在执行。" ); } } |
3 、代理类 Proxy 继承于 Subject 类,同时引用 RealSubject 类 |
public class Proxy :Subject { private RealSubject realSubject; public override void Request() { Console .WriteLine(" 通过代理类访问对象真实实体的方法。" ); if (realSubject == null ) { realSubject = new RealSubject (); } realSubject.Request(); } } |
4 、客户端代码 |
class Program { static void Main (string [] args) { Proxy proxy = new Proxy (); proxy.Request(); Console .Read(); } } |