Design patterns – Proxy

Proxy it’s a structural design pattern used to create additional layer to access an object. Imagine you have an object with time-consuming initialization and you want to do this initialization only when this object is really needed. In such case you can have proxy object to decide when to initialize.
Proxy class should implement the same interface as ‘real’ class and have a reference to ‘real’ object.

In our example we have SumOperation class. Calculate method in this class is time consuming. We would like to cache it, to not execute it always when somebody calls to save time. SumOperationProxy class is implemented therefore. Both of them implement IOperation interface.

Our ‘real’ class with the interface.

    /// <summary>
    /// Interface for 'real' class and proxy class
    /// </summary>
    interface IOperation
    {

        string GetName();
        int Parameter { get; }
        int Calculate();
    }
	
    /// <summary>
    ///'Real' class with time consuming Calculate method.
    /// </summary>
    class SumOperation : IOperation
    {
        public SumOperation(int i)
        {
            Parameter = i;
        }

        public int Parameter { get; private set; }

        public int Calculate()
        {
            int result = 0;
            for (int j = 1; j <= Parameter; j++)
            {
                result += j;
                System.Threading.Thread.Sleep(1000);
            }
            return result;
        }

        public string GetName()
        {
            return $"Sum operation for {Parameter}.";
        }
    }

Our proxy class.

    /// <summary>
    /// Proxy class to cache time consuming method in 'real' class.
    /// Implements the same interface like 'real' class.
    /// </summary>
    class SumOperationProxy : IOperation
    {
        SumOperation _operation = null;
        int _result = 0;

        public SumOperationProxy(int i)
        {
            _operation = new SumOperation(i);
        }

        public int Parameter
        {
            get
            {
                return _operation.Parameter;
            }
        }

        public int Calculate()
        {
            if (_result == 0)
                _result = _operation.Calculate();

            return _result;
        }

        public string GetName()
        {
            return _operation.GetName();
        }
    }

Proxy usage. Notice that Calculate call for the first time takes much time, but for the second time it is executed fast.

        public static void ProxyUsage()
        {
            //instantiates proxy object quickly
            var myOperationProxy = new SumOperationProxy(5);
            Console.WriteLine(myOperationProxy.GetName());

            //heavy operation performed for a long time
            Console.WriteLine(myOperationProxy.Calculate());

            //heavy operation performed quickly as was cached by proxy class
            Console.WriteLine(myOperationProxy.Calculate());
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s