// EN: The most common applications of the Proxy pattern are lazy // loading, caching, controlling the access, logging, etc. A Proxy can // perform one of these things and then, depending on the result, pass // the execution to the same method in a linked RealSubject object. // // RU: Наиболее распространёнными областями применения паттерна // Заместитель являются ленивая загрузка, кэширование, контроль доступа, // ведение журнала и т.д. Заместитель может выполнить одну из этих // задач, а затем, в зависимости от результата, передать выполнение // одноимённому методу в связанном объекте класса Реального Субъект. public void Request() { if (this.CheckAccess()) { this._realSubject = new RealSubject(); this._realSubject.Request(); this.LogAccess(); } }
static void Main(string[] args) { Client client = new Client(); Console.WriteLine("Client: Executing the client code with a real subject:"); RealSubject realSubject = new RealSubject(); client.ClientCode(realSubject); Console.WriteLine(); Console.WriteLine("Client: Executing the same client code with a proxy:"); Proxy proxy = new Proxy(realSubject); client.ClientCode(proxy); }
public Proxy(RealSubject realSubject) { this._realSubject = realSubject; }