Proxy - Design Pattern


Objective ๐ฏ
Provide a substitute for another object to control access to it.
Type โ
โBehavioral: Describes how objects interact/communicate between themselves.
โCreational: Describes how to instantiate an object without large and complex.
โ๏ธStructural: Describes how objects/classes are composed to form larger structures.
UML ๐
Participants ๐
โข Subject:
- Defines an interface common for both Real and Proxy subject so that Proxy can be used anywhere a Real object is expected.
โข RealSubject:
- Implements the real operations that proxy will represent.
โข Proxy:
- Maintains a reference to RealSubject
- Forward method calls to RealSubject
Sample Code ๐ฎ
Structural Example ๐๏ธ
public static class ProxyStructural
{
public static void Execute()
{
Proxy lProxy = new Proxy();
lProxy.Request();
}
}
internal abstract class Subject
{
public abstract void Request();
}
internal class RealSubject : Subject
{
public override void Request()
{
Console.WriteLine("Called RealSubject.Request()");
}
}
internal class Proxy : Subject
{
private RealSubject _RealSubject;
public override void Request()
{
if (_RealSubject == null)
_RealSubject = new RealSubject();
_RealSubject.Request();
}
}
Output
Real-world Example ๐ฅ
namespace Main.Proxy
{
public static class ProxyPractical
{
public static void Execute()
{
Calculator lCalculator = new ProxyCalculator();
Console.WriteLine($"Proxy Calculator - Calling method \"Add\" in the Real Calculator... Result: 5 + 7 = {lCalculator.Add(5,7)}");
Console.WriteLine($"Proxy Calculator - Calling method \"Add\" in the Real Calculator... Result: 5 - 7 = {lCalculator.Subtract(5, 7)}");
Console.WriteLine($"Proxy Calculator - Calling method \"Add\" in the Real Calculator... Result: 5 * 7 = {lCalculator.Multiply(5, 7)}");
Console.WriteLine($"Proxy Calculator - Calling method \"Add\" in the Real Calculator... Result: 5 / 7 = {lCalculator.Divide(5, 7)}");
}
}
public abstract class Calculator
{
public abstract float Add(float prNumber1, float prNumber2);
public abstract float Subtract(float prNumber1, float prNumber2);
public abstract float Multiply(float prNumber1, float prNumber2);
public abstract float Divide(float prNumber1, float prNumber2);
}
public class RealCalculator : Calculator
{
public override float Add(float prNumber1, float prNumber2)
{
return prNumber1 + prNumber2;
}
public override float Divide(float prNumber1, float prNumber2)
{
return prNumber1 / prNumber2;
}
public override float Multiply(float prNumber1, float prNumber2)
{
return prNumber1 * prNumber2;
}
public override float Subtract(float prNumber1, float prNumber2)
{
return prNumber1 - prNumber2;
}
}
public class ProxyCalculator : Calculator
{
private RealCalculator _RealCalculator;
public ProxyCalculator()
{
_RealCalculator = new RealCalculator();
}
public override float Add(float prNumber1, float prNumber2)
{
return _RealCalculator.Add(prNumber1, prNumber2);
}
public override float Divide(float prNumber1, float prNumber2)
{
return _RealCalculator.Divide(prNumber1, prNumber2);
}
public override float Multiply(float prNumber1, float prNumber2)
{
return _RealCalculator.Multiply(prNumber1, prNumber2);
}
public override float Subtract(float prNumber1, float prNumber2)
{
return _RealCalculator.Subtract(prNumber1, prNumber2);
}
}
}
Output
Source Code ๐ฒ
https://github.com/VictorLins/DesignPatterns
Subscribe to my newsletter
Read articles from Victor Lins directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Victor Lins
Victor Lins
I am a software engineer with over 10 years of experience in a huge variety of projects and architectures, some simple others considerably complex. Some of my main skills include Software Architecture, Web Development, ASP.NET Core, React.js, Entity Framework Core, Javascript, SQL and Web Services. I consider myself passionate about coding as I really fell excited about learning technologies and using them to automate and improve processes.