Proxy Design Pattern in C#

Back to Proxy description

Provides a surrogate or placeholder for another object to control access to it.

This structural code demonstrates the Proxy pattern which provides a representative object (proxy) that controls access to another similar object.

using System;

  // MainApp test application 
  class MainApp
  {
    static void Main()
    {
      // Create proxy and request a service 
      Proxy proxy = new Proxy();
      proxy.Request();

      // Wait for user 
      Console.Read();
    }
  }

  // "Subject" 
  abstract class Subject 
  {
    public abstract void Request();    
  }

  // "RealSubject" 
  class RealSubject : Subject
  {
    public override void Request()
    {
      Console.WriteLine("Called RealSubject.Request()");
    }
  }

  // "Proxy" 
  class Proxy : Subject
  {
    RealSubject realSubject;

    public override void Request()
    {
      // Use 'lazy initialization' 
      if (realSubject == null)
      {
        realSubject = new RealSubject();
      }

      realSubject.Request();
    }  
  }
Called RealSubject.Request()

Read next

Other Proxy examples