Subject类:
namespace Proxy__Pattern_Test { abstract class Subject { public abstract void Request(); } }
RealSubject类:真正的实体,继承于Subject类
namespace Proxy__Pattern_Test { class RealSubject:Subject { public override void Request() { Console.WriteLine("我才是真正的实体!"); } } }
Proxy类:代理类,继承于Subject类
namespace Proxy__Pattern_Test { class Proxy:Subject { private RealSubject realSubject; public override void Request() { if (realSubject == null) { realSubject = new RealSubject(); } realSubject.Request(); } } }
客户端类:TestMain
namespace Proxy__Pattern_Test { class TestMain { static void Main(string[] args) { Proxy proxy = new Proxy(); proxy.Request(); Console.ReadLine(); } } }