Design Patterns - Proxy

27 mei 2022 om 10:00 by ParTech Media - Post a comment

In our previous blog of the design pattern series, we had discussed the flyweight design pattern, which falls under the category of structural design patterns. Today, we will explore the last design pattern from the same category called Proxy Design Pattern.

Table of contents

  1. Introduction to Proxy design pattern,
  2. Implementation of Proxy design pattern
  3. Conclusion

Introduction to Proxy design pattern

As the name suggests, the proxy design pattern masks an original object and serves as an exposed object in lieu of it. The service layer of a WCF service and the controller layer of a Web API are some common examples. In this, the actual data is fetched to the client using the proxy classes.

Now you may wonder, couldn’t this be achieved using the adapter and bridge design patterns? As we have already discussed in our previous blogs, the Adapter design pattern is used to address the incompatibility issues where the code acts as a converter or a translator. On the other hand, the bridge design pattern is all about providing multiple ways to achieve a task.

However, when it comes to Proxy design pattern, it’s all about offering security. You do not expose the resource that has access to certain data. Instead, the data is obtained through the proxy class, which, in turn, accesses the resource.

Whenever we talk about proxy design pattern, it’s hard not to talk about the below 4 terms -

  • Virtual Proxy: Virtual proxy is where the resource’s object creation is handled by the proxy class. This is used mainly for better memory handling and performance.
  • Remote Proxy: Remote proxy is the concept that is used in web services or WCF, where the client accesses the resource by using a proxy class.
  • Protection Proxy: In the case where the resource needs to be secured, the client has to provide some security code for the authentication mechanism to protect the resource.
  • Smart Proxy: Smart proxy is more like an extension method in C# where users need to add certain functionality to the existing classes or modify certain aspects before accessing the resource.

Implementation of Proxy design pattern

The proxy design pattern mainly consists of

  • Subject
  • RealSubject
  • Proxy
  • Client

Let's understand this with the help of a program.

using System;

namespace PARTECH_Proxy
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var proxy = new Proxy();
            proxy.Execute();
            Console.ReadLine();
        }

        public interface Subject
        {
            void Execute();
        }

        public class RealSubject : Subject
        {
            public void Execute()
            {
                Console.WriteLine("Task Performed.");
            }
        }

        public class Proxy : Subject
        {
            Subject _subject;

            public void Execute()
            {
                if (_subject == null)
                {
                    _subject = new RealSubject();
                }

                _subject.Execute();
            }
        }

    }
}

Here,

The subject is an interface that has the declaration of the methods. These are those methods that need to be implemented by the resource as well as the proxy class.

RealSubject is the class that implements the Subject interface. It is the resource that has access to the required information, which, in turn, creates the logic to produce the information directly from it. This will not be exposed to the client.

Proxy is the wrapper class that is exposed to the client. It again implements the Subject interface and checks if the object of the real subject is present or not. If not present, it creates it and calls the actual implementation of it.

In the above example, the main method acts as the client where the object for the proxy class has been created, and the Execute method is called from there. On running the above code, the below output gets displayed on the console window.

Let's implement the proxy design pattern for a real-time scenario. We are going to implement the functionality of a calculator, where we do not want to expose the actual logic implementation class to the client. Instead, we will mask it using a proxy class that serves as the external contractor to the client. Let's now see how to implement it.

using System;

namespace PARTECH_Proxy
{
    internal class Program
    {
        public interface ICalculator
        {
            double Add(double input1, double input2);
            double Subtract(double input1, double input2);
            double Multiple(double input1, double input2);
            double Divide(double input1, double input2);
        }

        public class Calculator : ICalculator
        {
            public double Add(double input1, double input2) 
            { 
                return input1 + input2; 
            }
            public double Subtract(double input1, double input2) 
            { 
                return input1 - input2; 
            }
            public double Multiple(double input1, double input2) 
            { 
                return input1 * input2; 
            }
            public double Divide(double input1, double input2) 
            { 
                return input1 / input2;
            }
        }

        public class ProxyCalc : ICalculator
        {
            Calculator calc = new Calculator();

            public double Add(double input1, double input2)
            {
                return calc.Add(input1, input2);
            }
            public double Subtract(double input1, double input2)
            {
                return calc.Subtract(input1, input2);
            }
            public double Multiple(double input1, double input2)
            {
                return calc.Multiple(input1, input2);
            }
            public double Divide(double input1, double input2)
            {
                return calc.Divide(input1, input2);
            }
        }
        static void Main(string[] args)
        {
            var calculator = new ProxyCalc();
            Console.WriteLine($"Addition of 5 and 10 is { calculator.Add(5, 10) }");
            Console.WriteLine($"Difference of 10 and 5 is { calculator.Subtract(10, 5) }");
            Console.ReadLine();
        }
}
}

Here, ICalculator is the subject that has the declarations of the methods. The calculator is the RealSubject where the actual implementation of the logic has been added. ProxyCalc is the proxy class that implements the ICalculator. It internally creates the Calculator object and accesses the respective functionality in the method implementation.

The main method is the client here, and it creates an object for the proxy calculator. It calls two methods in the proxy calc class (Add and subtract). On executing the above code, the below output gets displayed.

Conclusion

And that’s what a Proxy Structural design pattern is all about. In this post, we have also seen how to implement it in a real-world scenario. With this, we have covered all the design patterns under the structural design pattern category. Stay tuned for the next blog on the Design Patterns series.