Thursday, 26 December 2013

Proxy Design Pattern

Proxy Design Pattern

Proxy pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. The proxy design pattern is used to provide a surrogate object, which references to other object. In this article, I would like share what is proxy pattern and how is it work?

What is Proxy Pattern

The proxy design pattern is used to provide a surrogate object, which references to other object.
Proxy pattern involves a class, called proxy class, which represents functionality of another class.

Proxy Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the proxy design pattern is given below:

 
 The classes and/or objects participating in this pattern are:
  • Proxy   (MathProxy)
    • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
    • provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.
    • controls access to the real subject and may be responsible for creating and deleting it.
    • other responsibilites depend on the kind of proxy:
      • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
      • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.
      • protection proxies check that the caller has the access permissions required to perform a request.
  • Subject   (IMath)
    • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject   (Math)
    • defines the real object that the proxy represents. 

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:
  1. IClient- Subject Interface.
  2. RealClient - RealSubject Class.
  3. ProxyClient - Proxy Class.

C# - Sample Code

using System;

namespace DoFactory.GangOfFour.Proxy.RealWorld
{
  /// <summary>
  /// MainApp startup class for Real-World
  /// Proxy Design Pattern.
  /// </summary>
  class MainApp
  {
    /// <summary>
    /// Entry point into console application.
    /// </summary>
    static void Main()
    {
      // Create math proxy
      MathProxy proxy = new MathProxy();

      // Do the math
      Console.WriteLine("4 + 2 = " + proxy.Add(4, 2));
      Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2));
      Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2));
      Console.WriteLine("4 / 2 = " + proxy.Div(4, 2));

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

  /// <summary>
  /// The 'Subject interface
  /// </summary>
  public interface IMath
  {
    double Add(double x, double y);
    double Sub(double x, double y);
    double Mul(double x, double y);
    double Div(double x, double y);
  }

  /// <summary>
  /// The 'RealSubject' class
  /// </summary>
  class Math : IMath
  {
    public double Add(double x, double y) { return x + y; }
    public double Sub(double x, double y) { return x - y; }
    public double Mul(double x, double y) { return x * y; }
    public double Div(double x, double y) { return x / y; }
  }

  /// <summary>
  /// The 'Proxy Object' class
  /// </summary>
  class MathProxy : IMath
  {
    private Math _math = new Math();

    public double Add(double x, double y)
    {
      return _math.Add(x, y);
    }
    public double Sub(double x, double y)
    {
      return _math.Sub(x, y);
    }
    public double Mul(double x, double y)
    {
      return _math.Mul(x, y);
    }
    public double Div(double x, double y)
    {
      return _math.Div(x, y);
    }
  }
}

Output
4 + 2 = 6
4 - 2 = 2
4 * 2 = 8
4 / 2 = 2
 
There are various kinds of proxies, some of them are as follows:
  1. Virtual proxies : Hand over the creation of an object to another object
  2. Authentication proxies : Checks the access permissions for a request
  3. Remote proxies : Encodes requests and send them across a network
  4. Smart proxies : Change requests before sending them across a network

When to use it?

  1. Objects need to be created on demand means when their operations are requested.
  2. Access control for the original object is required.
  3. Allow to access a remote object by using a local object(it will refer to a remote object).

 

 

No comments:

Post a Comment