Monday 20 December 2010

Proxy Pattern

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


Proxy and Subject implement the same Subject interface so that the when the client "thinks" it is calling the Request() on the RealSubject it is actually calling the Proxy, which forwards the Request() call to the RealSubject.
Proxies are useful for controlling access to the RealSubject or for handling access to a remote RealSubject.  The Proxy sits in between the Client and RealSubject.  In the code example below the client uses the Calculator through the ICalculator interface.  Because ProxyCalculator implements the same interface, we can swap in a ProxyCalculator which just delegates all calls to the Caclulator as long as the user is authorised to use the real Calculator.


using System;
 
namespace Proxy
{
     class Program
     {
           static void Main(string[] args)
           {
                ICalculator calculator =
                new ProxyCalculator("Jason");              
                Console.WriteLine(calculator.Add(1, 2));
                Console.ReadLine();
           }
     }
 
     public interface ICalculator
     {
           double Add(double num1, double num2);
           double Subtract(double num1, double num2);
           double Divide(double num1, double num2);
           double Multiply(double num1, double num2);     
     }
 
     public class RealCalculator : ICalculator
     {
           public double Add(double num1, double num2)
           {
                return num1 + num2;
           }
 
           public double Subtract(double num1, double num2)
           {
                return num1 - num2;
           }
 
           public double Divide(double num1, double num2)
           {
                return num1 / num2;
           }
 
           public double Multiply(double num1, double num2)
           {
                return num1 * num2;
           }
     }
 
     public class ProxyCalculator : ICalculator
     {
           RealCalculator _calc;
 
           public ProxyCalculator(string user)
           {
                if (user == "Jason")
                {
                      _calc = new RealCalculator();
                }
           }
 
           public double Add(double num1, double num2)
           {
                if (_calc != null)
                {
                     return _calc.Add(num1, num2);
                }
                throw new InvalidOperationException
                ("You're not Jason!");
           }
 
           public double Subtract(double num1, double num2)
           {
                if (_calc != null)
                {
                     return _calc.Subtract(num1, num2);
                }

                throw new InvalidOperationException
                ("You're not Jason!");

           }
 
           public double Divide(double num1, double num2)
           {
                if (_calc != null)
                {
                     return _calc.Divide(num1, num2);
                }
                throw new InvalidOperationException
                ("You're not Jason!");
           }
 
           public double Multiply(double num1, double num2)
           {
                if (_calc != null)
                {
                     return _calc.Multiply(num1, num2);
                }
                throw new InvalidOperationException
                ("You're not Jason!");
           }
     }
}

No comments:

Post a Comment