Proxy Design Pattern

Introduction:
Sometimes you become into a case where you does not or can not reference an object directly, but you still want to interact with that object to do some job. The intent of the proxy design pattern is to control access to an object by providing placeholder for it. Below example will make the idea clear to you.
Example:

ProxyDesignPattern

 

static void Main()
{
// Create proxy Class
MathProxy proxy = new MathProxy();
// Call Methods
Console.WriteLine(“4 + 2 = ” + proxy.AddOperation(4, 2));
Console.WriteLine(“4 – 2 = ” + proxy.SubOperation(4, 2));
Console.WriteLine(“4 * 2 = ” + proxy.MultiplyOperation(4, 2));
Console.WriteLine(“4 / 2 = ” + proxy.DivsionOperation(4, 2));
// Exit
Console.ReadKey();
}
public interface IMath
{
double AddOperation(double a, double b);
double SubOperation(double a, double b);
double MultiplyOperation(double a, double b);
double DivsionOperation(double a, double b);
}
class Math : IMath
{
public double AddOperation(double a, double b)
{
return a + b;
}
public double SubOperation(double a, double b)
{
return a – b;
}
public double MultiplyOperation(double a, double b)
{
return a * b;
}
public double DivsionOperation(double a, double b)
{
if (b == 0)
return 0;
return a / b;
}
}
class MathProxy : IMath
{
private Math mathObject = new Math();
public double AddOperation(double a, double b)
{
return mathObject.AddOperation(a, b);
}
public double SubOperation(double a, double b)
{
return mathObject.SubOperation(a, b);
}
public double MultiplyOperation(double a, double b)
{
return mathObject.MultiplyOperation(a, b);
}
public double DivsionOperation(double a, double b)
{
return mathObject.DivsionOperation(a, b);
}
}

 

Download Code