代理模式(Proxy):為其他對象提供一種代理以控制對這個對象的訪問。
namespace ProxyDesignPattern { public abstract class Subject { public abstract void Request(); } public class RealSubject:Subject { public override void Request() { Console.WriteLine("真實的請求"); } } public class Proxy:Subject { RealSubject realSubject; public override void Request() { if (realSubject == null) { realSubject = new RealSubject(); } realSubject.Request(); } } }View Code
測試代碼:
Proxy proxy = new Proxy(); proxy.Request();View Code