Understanding Proxy Design Pattern In C#

Salim Alam
3 min readNov 27, 2019

--

Photo by Jessica Rockowitz on Unsplash

As developers, the proxy design pattern is a useful design pattern to have in our arsenal. Let us try to understand the proxy design pattern in detail.

At first, let us take a look at the _”Gang of four”_ definition:

“Provide a surrogate or placeholder for another object to control access to it.”

My first thought after reading the statement was, why would I increase the complexity of reaching to my object? But when I saw the following UML diagram, I realized I had implemented something similar (not in exact but close enough) without realizing at that time that it was the proxy design pattern.

UML diagram Proxy Design Pattern in Design patterns: elements of reusable object-oriented software

In the diagram, we have three classes Subject, Proxy, and RealSubject. From the diagram, we can understand that Subject must be an abstract class, and both Proxy and RealSubject implements Subject.

“ please note that the subject class can also be an interface.”

From the diagram, we can also see that the Request method of our Proxy class delegates out or calls out the Request Method of RealSubject. However, this part is a little misleading. We only delegate to RealSubject under special conditions that we define.

So you can see that proxy class acts as a surrogate to RealSubject, the proxy layer controls how RealSubject can be accessed.

The proxy pattern is categorized based on scenarios it is applied.

Virtual Proxy: Provides a proxy layer that is responsible for creating objects on-demand, which otherwise would be expensive to create directly.

In virtual proxy, our proxy object is a lightweight version of our RealSubject.

For example, say we have a Document reader client that loads image objects, and our RealImage class displays high-resolution images. In contrast, our ProxyImage class displays a low resolution of the same image depending on bandwidth.

In the above code block, we have an IImage interface, and this is the implementation of Subject from the UML diagram. For simplicity, the IImage interface has one method Display() that returns an Image type.

Both ProxyImage and RealImage are concrete implementations of IImage interface and represents Proxy and RealSubject from the UML.

The Display method in ProxyImage class, when called by the client, the network bandwidth is checked, and if the bandwidth is less than the specified bandwidth limit, the method gets executed. However, if the bandwidth is higher than the set limit, the call gets delegated to the Display() method of RealImage.

Protection Proxy: Provides a proxy layer that checks the rights and permission to call specific methods and properties.

As an example of protection proxy in our context, we can perform Authorization checks in the display method of ProxyImage Class. The call gets delegated to RealImage.Display() if Authorized else we can return a NotAuthorized Image Object.

Remote Proxy: Provides a proxy layer for real objects in a remote location.

In the context of our example for remote proxy, the display method of RealImage would return an image object and initialized after getting it from a remote location, say Azure Blob storage.

To summarize, the proxy pattern allows us to create a placeholder object for real objects which are expensive to create, are in a remote location, or to place an authorization for real objects behaviors and values without changing the interface.

--

--

Salim Alam

Software Developer, Technical Lead (.Net Technologies)