Created
February 3, 2016 06:40
-
-
Save sudipto80/8776d33d53750c6b6db4 to your computer and use it in GitHub Desktop.
proxy pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment