Proxy - Design Pattern

Victor LinsVictor Lins
2 min read

Objective ๐ŸŽฏ

Provide a substitute for another object to control access to it.

image.png

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 ๐Ÿ“

image.png

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 ๐Ÿ›๏ธ

image.png

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

image.png

Real-world Example ๐Ÿ”ฅ

image.png

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

image.png

Source Code ๐ŸŽฒ

https://github.com/VictorLins/DesignPatterns

0
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.