Monday, November 12, 2007

* Patterns : Proxy Structural Pattern

Proxy Structural Pattern or Proxy object can be used in multiple ways. Some of them could be following :

  • Act as a simpler interface to a complex object.
  • Can be used to transform call from one format to another.
  • can be used to load balance multiple objects.
  • can be used to track concurrency or object numbers.

Consider this example where a Proxy Calculator is acting as interface to Real Calculator. Client makes all calls to Proxy and proxy's responsibility is to route the calls both ways. This proxy template can be used to perform any of the above mentioned tasks with some modifications.

Proxy

Client is aware of only ProxyCalculator.

public class ProxyCalculator : ICalculator
{
    #region ICalculator Members

    RealCalculator rc;

    public ProxyCalculator()
    {
        rc = new RealCalculator();
    }

    public int Add(int firstNum, int secondNum)
    {
        return rc.Add(firstNum, secondNum);
    }

    public int Multiply(int firstNum, int secondNum)
    {
        return rc.Multiply(firstNum, secondNum);
    }

    #endregion
}

ProxyCalculator creates the reference of RealCalculator and forwards all client calls to RealCalculator.

Download Proxy Structural Pattern Source Code

No comments:

Post a Comment