How to Implement Proxy Pattern Using C# (C Sharp)?

Few objects in your application might be very expensive in terms of time consumption and volume of memory it occupies. In such cases, loading those objects at the beginning of your application even before User requests for it is unnecessary. Hence you need some source which delays creation of such objects until it is really required. This is achieved using Proxy design pattern. This pattern is used during various situations. Few of them are mentioned below:



• If you have to display result of a computation that takes more time to complete, you can show intermediate results using proxy pattern instead of showing a blank screen to the User until the result arrives.
• If images of your page takes more time to load, until it gets loaded you can show some dummy images.
• You can also use proxy pattern to load the object only if User has permission to access the object.

In this example, you are going to analyze the second scenario mentioned above. Your web pages might have images contained in it. Normally loading images is time consuming when compared to loading text content. Hence most of the web pages hosted in internet will display text first and then slowly load the images. In the place of images, it will initially display a blank rectangular box alone which will then be replaced with the original image. Hope you might have experienced it. How is this postponement of image loading incorporated? It is done using Proxy pattern.

Since the original image requires time to load, you first display a proxy image (a rectangular box). After the original image is loaded, you replace the proxy image with this original image. This is explained using the code shown below:

First you create an interface containing a method called displayImage() which is overridden by methods in two classes, one representing the proxy and the other representing class containing original image. This is demonstrated in the code below:

public interface ImageInterface{
Image displayImage();
}
public class proxyImage:ImageInterface {
public proxyImage(){
Console.WriteLine(“In proxyImage Class”);
}
public Image displayImage(){
Bitmap img = new Bitmap(“proxyImg.gif”);
return img;
}
}
public class originalImage:ImageInterface {
public originalImage(){
Console.WriteLine(“In originalImage Class”);
}
public Image displayImage(){
Bitmap img = new Bitmap(“originalImg.gif”);
return img;
}
}

In this example, proxyImg.gif contains a blank rectangular box and originalImg.gif contains the actual image to be loaded. First, proxyImg.gif has to be loaded and after a certain time period originalImg.gif has to be loaded. This is achieved using the code sample below:

public class proxyClass{
private bool loadOriginalImage;
private Timer timerObj;

public proxyClass {
timerObj = new Timer( new TimerCallback(endTimer), this, 10000, 0);
}

private void endTimer(object obj) {
loadOriginalImage = true;
timerObj.Dispose();
}

public Image populateImage() {
ImageInterface imageObj;
if(loadOriginalImage) {
imageObj = new originalmage();
}
else {
imageObj = new proxyImage();
}
return imageObj.displayImage();
}
}

In this proxyClass, you decide when to display proxy image and when to display actual image. Here you assume that your original image requires 10 seconds to load. You initiate a timer and until the timer expires, you show the proxy image. When 10 second elapses, the timer expires and the original image will be shown. You incorporated this logic in the code shown above.

You have initiated a timer for 10 seconds inside the constructor of proxyClass. This timer will call the endTimer method when the timer expires. This endTimer method will set the Boolean variable loadOriginalImage to true. This Boolean variable is used in the populateImage method. In this method you check for the loadOriginalImage value. It will be false by default. Hence proxy image will be populated. After 10 seconds, the Boolean variable will be set to true and at that point of time, original image will be populated using this method.

Where will these images be shown? Assume that you have a web page with a button with caption “Load Image”. On click of this button, for the first 10 seconds proxy image has to be displayed and then original image has to be loaded. You have this entire logic built in the populateImage method of proxyClass. Hence you have initiate call to this method. How and where will you do it? That is shown in the code sample below:

public MainForm() {
InitializeComponent();
proxyClass proxyInstance = new proxyClass();
}
void LoadImageButton_Click(object sender, EventArgs e) {
samplePicture.Image = proxyInstance.populateImage();
}

In the MainForm, you create an instance of proxyClass and on click of “Load Image” button you call the populateMethod of proxyClass. This method will in turn associate with other classes and load the appropriate images based on the timer set.

| Design Guideline for C# Structs (C Sharp) | Design Patterns – Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.