Proxy Design Pattern Real-time Example

Proxy Design Pattern Real-time Example – Virtual Proxy

In this article, I am going to discuss the Proxy Design pattern Real-time Example – Virtual Proxy in C#. Please read our previous article where we discussed the Proxy Design Pattern in C# with examples.

Proxy Design Pattern Real-time Example in C# – Virtual Proxy

A Virtual Proxy is a placeholder for objects which are expensive to create. The real object is only created when a client requests or accesses an object for the first time. Let us understand this with one real-time example. Please have a look at the following image. On the right-hand side, you can see the System A which has one image (Tiger image) of 200 MB. On the left-hand side, you can see the client. In between the client and System A, there is System B which is acts as the virtual proxy.

Proxy Design Pattern Real-time Example in C# - Virtual Proxy

Let say, for the first time, the client sends a request to System B (Virtual Proxy) to display the Tiger Image. What the Virtual Proxy (i.e. System B) will do is, first it will check whether the real image object is there in the virtual proxy or not. If the real image object is not there, then in step1 it will create the real image object and load the image from the disk and in step2 it will call the display image method on the real image object. Virtual Proxy also holds the real image object which is created in step1. The step1 i.e. creating the real image object and loading the image from the disk is an expensive operation.

Let say the client makes the same request for the second time to the virtual proxy to display the Tiger Image. Now, what the virtual proxy will do is, check whether the real-image object is there or not and it found the real-image object is there in the virtual proxy (this is because in the first request the Virtual Proxy holds the real-image object). So, what the virtual proxy will do is, it will not execute the step1 i.e. it will not create the real-image object and loading the image from the disk. Instead, it will use the existing real-image object and call the Display Image method i.e. step2. So, in this way, using the proxy design pattern, we can avoid creating an expensive object again and again.

Class Diagram:

Please have a look at the following diagram.

Implementation of Real-Time Example of Proxy Design Pattern in C#:

Implementation of Real-Time Example of Proxy Design Pattern in C#:

Let us implement the above real-time example step by step using the proxy design pattern. As we know the proxy design pattern involves three components such as Subject, Real Object, and Proxy Object. Let us implement the above component one by one.

Step1: Creating Subject

This is going to be an interface. So, create an interface with the name IImage and then copy and paste the following code in it. This interface provides the functionalities which are going to be implemented by the Real Object and Proxy Object concrete class. In our example, the interface defines one method i.e. DisplayImage/

namespace ProxyDesignPattern
{
    public interface IImage
    {
        void DisplayImage();
    }
}
Step2: Creating Real-Object

This is going to be a concrete class and this class implements the IImage interface and provide the implementation for the DisplayImage method. So, create a class file with the name RealImage.cs and then copy and paste the following code in it. This constructor of RealImage class takes the file name as a parameter and then loads the file from the disk.

using System;
namespace ProxyDesignPattern
{
    public class RealImage : IImage
    {
        private string Filename { get; set; }

        public RealImage(string filename)
        {
            Filename = filename;
            LoadImageFromDisk();
        }

        public void LoadImageFromDisk()
        {
            Console.WriteLine("Loading Image : " + Filename);
        }

        public void DisplayImage()
        {
            Console.WriteLine("Displaying Image : " + Filename);
        }
    }
}

Note: Here the object creation process in an expensive operation. This is because at the time of object creation it will load the image from the disk. The LoadImageFromDisk method is used to load the image from the disk. The DisplayImage method simply used to display the image.

Step3: Creating Proxy 

This is going to be a concrete class and it also implements the IImage interface and provides the implementation for the DisplayImage method. So, create a class file with the name ProxyObject.cs and then copy and paste the following code in it. As part of the DisplayImage method, first, we are checking whether the realImage instance is null or not. If null, then we are creating the instance and then on the realImage instance, we are calling the DisplayImage method. On the other hand, if the realImage instance is not null, then it will not create the instance instead it will use the existing realImage instance to call the DisplayImage method.

namespace ProxyDesignPattern
{
    public class ProxyImage : IImage
    {
        private RealImage realImage = null;
        private string Filename { get; set; }

        public ProxyImage(string filename)
        {
            Filename = filename;
        }
        public void DisplayImage()
        {
            if(realImage == null)
            {
                realImage = new RealImage(Filename);
            }

            realImage.DisplayImage();
        }
    }
}
Step4: Client

Please modify the Main method as shown below. First, we creating the object of ProxyImage to display the Tiger Image and then calling the DisplayImage method three times. In this case, the first call to DisplayImage method will create the RealImage instance and hence it will load the image from the disk. But from the 2nd call onwards to the DisplayImage method, it will use the existing RealImage instance and hence it will not load the image from the disk. This process is also the same for the 2nd proxy object creation to display the Lion Image.

using System;
namespace ProxyDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            IImage Image1 = new ProxyImage("Tiger Image");
            
            Console.WriteLine("Image1 calling DisplayImage first time :");
            Image1.DisplayImage(); // loading necessary

            Console.WriteLine("Image1 calling DisplayImage second time :");
            Image1.DisplayImage(); // loading unnecessary

            Console.WriteLine("Image1 calling DisplayImage third time :");
            Image1.DisplayImage(); // loading unnecessary

            Console.WriteLine();
            IImage Image2 = new ProxyImage("Lion Image");

            Console.WriteLine("Image2 calling DisplayImage first time :");
            Image2.DisplayImage(); // loading necessary

            Console.WriteLine("Image2 calling DisplayImage second time :");
            Image2.DisplayImage(); // loading unnecessary

            Console.ReadKey();
        }
    }
}

Output:

Implementation of Virtual Proxy Real-Time Example in C#

In the next article, I am going to discuss the Flyweight Design Pattern in C# with real-time examples. Here, in this article, I try to explain the Proxy Design Pattern Real-Time Example in C#. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *