Thursday, September 21, 2017

Proxy Design Pattern

Alright lets discuss about one of the structural pattern - Proxy Design Pattern.

Lets first understand the following.

Which Problem does this pattern solve?
Ans.This pattern is predominantly used when we need to control access or resource which is expensive to create it.

Let me explain it breifly.

Suppose there is a Concrete class which has 5 methods. For some reason creating an instance of this class is preety expensive.(When I mean Expensive. Creation of the class instance will take up a lot of memory or resource or may be something else)

So you create a Proxy class. Which will delay the instantiation for a breif period of time.

Below is the Example.

Lets create an Interface ISubject - This interface should and must be inherited by Both Proxy class and Concrete class.

   public interface ISubject
    {
        void Request();

        void Response();
    }

So lets create a Concrete class called - RealSubject which inherits ISubject.

    public class RealSubject : ISubject
    {
        public void Request()
        {
            Console.WriteLine("Called the Real Object");
        }

        public void Response()
        {
            Console.WriteLine("Called the Real Response");
        }
    }

So lets create a Proxy class called - ProxySubject which inherits ISubject -  (We Created this class assuming Creating an Instance of  RealSubject class directly would be more expensive)

If you closely look at the implementation of the ProxySubject - We are doing a Lazy Initialization of the RealSubject object in the Method Implementations -  So this is delay in Initialization that the pattern helps us achieve.

Also we are doing Lazy Initialization so that once we have created a instance of RealSubject we should use it for all the other methods. Else the whole purpose is Proxy Pattern goes for a TOSS and we would end up instantiating each time for each method in the ProxySubject class.

    public class ProxySubject : ISubject
    {
        private RealSubject _subject;

        public void Request()
        {
            //Lazy Initialization
            if (_subject == null)
            {
                _subject = new RealSubject();
            }
            _subject.Request();
        }

        public void Response()
        {
            //Lazy Initialization
            if (_subject == null)
            {
                _subject = new RealSubject();
            }
            _subject.Response();
        }
    }

Finally after all the set up lets instantiate the Proxy class and reap the benefits of the Pattern.

using System;

namespace ProxyDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {

            //Proxy Pattern - Provides a Surrogacy -or placeholder for controlling access of the subject
            var subject = new ProxySubject();
            subject.Request();
            subject.Response();
            Console.ReadLine();
        }
    }
}

Happy Coding :) :) :)