Back to: Design Patterns in C# With Real-Time Examples
Proxy Design Pattern in C# with Examples
In this article, I am going to discuss the Proxy Design Pattern in C# with Examples. Please read our previous article where we discussed the Composite Design Pattern in C# with Examples. The Proxy Design Pattern falls under the category of Structural Design Pattern. As part of this article, we are going to discuss the following pointers.
- What is Proxy Design Pattern?
- Understanding the different types of Proxies.
- Proxy Design Pattern Real-time Examples.
- Why do we need Proxy Design Pattern in C#
- Implementation of Proxy Design Pattern in C#.
- Understanding the class diagram of the Proxy Design Pattern.
- When to use the Proxy Design Pattern in real-time applications?
What is Proxy Design Pattern?
According to the Gang of four definitions, Proxy Design Pattern provides a surrogate (act on behalf of another) or placeholder for another object to control access to it.
The Proxy Design Pattern allows us to create a class that represents the functionality of other classes. The proxy could interface with anything such as a network connection, a large object in memory, a file, or some other resources that are expensive or impossible to duplicate.
We can also say that the Proxy is the object which is being called by the client to access the real object behind the scene. Proxy means in place of or on behalf of. That means, In Proxy Design Pattern, a class represents the functionality of another class.
Let us understand the above definition with one example. Please have a look at the following diagram for a better understanding of the Proxy Design Pattern in C#. As you can see in the following image, when the client wants to consume some methods of the Real Object, then he/she needs to go via the Proxy object. That means the client will call the method of the Proxy object and the proxy will be responsible to call the method of the Real Object.
Types of Proxies:
There are three types of proxies. They are as follows.
- Virtual Proxy: A virtual proxy is a placeholder for “expensive to create” objects. The real object is only created when a client first requests or accesses the object.
- Remote Proxy: A remote proxy provides local representation for an object that resides in a different address space.
- Protection Proxy: A protection proxy control access to a sensitive master object. The surrogate object checks that the caller has the access permissions required prior to forwarding the request.
Real-Time Example to Understand Proxy Design Pattern:
Please have a look at the following diagram for a better understanding of the Proxy Design Pattern. On the right-hand side, you can see the State Bank of India, and on the left-hand side, you can see a person called Anurag. Anurag has an account in the State Bank of India. In earlier days, let’s say 1980, Anurag wants to withdraw money from his account. Then what he has to do? He has to carry his passbook and go to the bank. Then he has to fill out the form and needs to stand in the queue. Once his turn comes, he has to give the form and bank passbook to the bank employee and then the bank employee verifies the form and his passbook, and if everything is fine then the bank employee gives the required money to Anurag.
Let’s assume Anurag wants to withdraw money nowadays. So, instead of going to the bank what he can do nowadays is, just walks to the nearest ATM with his Debit Card. Then he inserts his Debit Card and enters the pin and the amount to withdraw. The ATM will then communicates with the bank, validate the Pin and Amount and if everything is fine then the ATM will give the money to Anurag immediately. So, instead of going to the bank, Anurag can withdraw Money from the ATM. So, here the Bank is the Real Object and ATM is the Proxy. And I think this is the best Real-Time Example of the Proxy Design Pattern.
Why do we need Proxy Design Pattern in C#?
Let us understand the need for the Proxy Design Pattern with the example of a Proxy Server. A server that sits between a client application such as a Web Browser and a Real Server is called a Proxy Server. The Proxy Server intercepts all the incoming requests for the Real Server to see if it can fulfill the requests by itself. If not then it will forward the requests to the Real Server. The Proxy server has two main objectives. They are as follows:
1. Improve Performance using Proxy Server:
The Proxy Servers can drastically improve the performance of the application. This is because it saves the results of a request for a certain period of time. For example, let’s say we have two users called X and Y and they want to access a particular resource through the Proxy Server. First, user X requests for a particular resource (let’s say a list of employees), and the Proxy Server gets the data from the real server, and before sending the data to the client, it caches that resource (list of employees) for a certain amount of time. Later, user Y also requests for the same resource, then the Proxy Server instead of forwarding that request to the Real Server (which is a time-consuming operation), can simply return the data from the Cache which will ultimately improve the performance of the application.
2. Filter Requests using Proxy Server:
The Proxy Servers can also be used to filter incoming requests. For example, a company might use a Proxy Server to prevent its employees from accessing a specific set of websites such as Facebook, Twitter, etc.
Implementation of Proxy Design Pattern in C# (Protection Proxy):
Please have a look at the following diagram. As you can see in the following diagram, on the right-hand side, we have a shared computer that has a shared folder. On the left-hand side, we have employees who are working on a software farm. The shared computer contains a shared folder that contains confidential information and only the employee having the role of Manager and CEO can access this shared folder and perform the Read-Write operations. On the other hand, if the employee is a developer, then it should not allow access to the shared folder. That is we need to do some kind of protection. In scenarios like this, the Protection Proxy can be handy.
What we can do here is, in between the employees and the shared computer we need to introduce the Folder Proxy. What this Folder proxy can do is, it will check if the employee’s role is Manager or CEO, then it allows the employee to access the shared folder and perform the read-write operation. On the other hand, if the employee role is Developer, then it will say you don’t have permission to access this folder. That kind of protection logic we can write using the Proxy Design Pattern. Here, within the Folder Proxy, we need to write the logic to filter the incoming requests.
Now, I hope you understood the Proxy Design Pattern. Let us proceed and implement the above example using the Proxy Design Pattern in C# step by step.
Step 1: Creating the Employee class
Create a class file with the name Employee.cs and then copy and paste the following code into it. As you can see, this is a very simple class having three properties i.e. Username, Password, and Role and we are also having one Parameterized constructor to initialize these data members.
namespace ProxyDesignPattern { public class Employee { public string Username { get; set; } public string Password { get; set; } public string Role { get; set; } public Employee(string username, string password, string role) { Username = username; Password = password; Role = role; } } }
Step 2: Creating Subject Interface
Create an interface with the name ISharedFolder.cs and then copy and paste the following code into it. This interface defines the common methods which are going to be implemented by the Real Object as well as the Proxy class. Here, you can see, we have declared one method called PerformRWOperations which is going to be implemented by both Real Object and Proxy Object.
namespace ProxyDesignPattern { // The Subject interface declares common operations for both RealSubject and the Proxy. // As long as the client works with RealSubject using this interface, // you will be able to pass it a proxy instead of a real subject. public interface ISharedFolder { void PerformRWOperations(); } }
Step3: Creating Real Subject
Create a class file with the name SharedFolder.cs and then copy and paste the following code into it. This class implements the subject (ISharedFolder) interface and provides implementations for the PerformRWOperations. This is the Real Object where the Manager or CEO going to perform the Read and Write Operations.
using System; namespace ProxyDesignPattern { // The RealSubject contains some core business logic. // Usually, RealSubjects are capable of doing some useful work which may be very slow or sensitive // A Proxy can solve these issues without any changes to the RealSubject's code. public class SharedFolder : ISharedFolder { public void PerformRWOperations() { Console.WriteLine("Performing Read Write operation on the Shared Folder"); } } }
Step4: Creating the Proxy Object
Create a class file with the name SharedFolderProxy.cs and then copy and paste the following code into it. This class also implemented the Subject (ISharedFolder) interface and provides implementations for the PerformRWOperations. This Proxy class also holds a reference to the real object i.e. private ISharedFolder folder. To the constructor of this class, we are passing the Employee object and within the PerformRWOperations method, we are checking whether the employee Role is either CEO or MANAGER. If the employee Role is either CEO or MANAGER, then we are Initializing the real object and calling the PerformRWOperations method on the real object else we are displaying the message that you don’t have permission to access this shared folder.
using System; namespace ProxyDesignPattern { // The Proxy has an interface identical to the RealSubject. class SharedFolderProxy : ISharedFolder { private ISharedFolder folder; private Employee employee; public SharedFolderProxy(Employee emp) { employee = emp; } public void PerformRWOperations() { if (employee.Role.ToUpper() == "CEO" || employee.Role.ToUpper() == "MANAGER") { folder = new SharedFolder(); Console.WriteLine("Shared Folder Proxy makes call to the RealFolder 'PerformRWOperations method'"); folder.PerformRWOperations(); } else { Console.WriteLine("Shared Folder proxy says 'You don't have permission to access this folder'"); } } } }
Step5: Client code
The Client is going to use the Proxy Object and the Proxy object is going to call the Actual Object behind the scene. In our example, the Main Method of the Program class is going to be the Client, So, Please modify the Main method of the Program class as shown below.
using System; namespace ProxyDesignPattern { class Program { static void Main(string[] args) { Console.WriteLine("Client passing employee with Role Developer to folderproxy"); Employee emp1 = new Employee("Anurag", "Anurag123", "Developer"); SharedFolderProxy folderProxy1 = new SharedFolderProxy(emp1); folderProxy1.PerformRWOperations(); Console.WriteLine(); Console.WriteLine("Client passing employee with Role Manager to folderproxy"); Employee emp2 = new Employee("Pranaya", "Pranaya123", "Manager"); SharedFolderProxy folderProxy2 = new SharedFolderProxy(emp2); folderProxy2.PerformRWOperations(); Console.Read(); } } }
Output:
Understanding the Class or UML Diagram Proxy Design Pattern:
In order to understand the Class or UML Diagram of the Proxy Design Pattern in C#, please have a look at the following image.
As shown in the above diagram, there are four participants involved in the Proxy Design Pattern. They are as follows:
- Subject (ISharedFolder): This is an interface that defines the members that are going to be implemented by the RealSubject and Proxy class so that the Proxy can be used by the client instead of the RealSubject. In our example, it is the ISharedFolder interface.
- RealSubject (SharedFolder): This is a class that we want to use more efficiently by using the proxy class. This class should implement the Subject Interface. In our example, it is the SharedFolder class.
- Proxy (SharedFolderProxy): This is a class that holds a reference to the RealSubject class and can access RealSubjecr class members as required. It must implement the same interface as the RealSubject so that the two can be used interchangeably. In our example, it is the SharedFolderProxy class.
- Client: This Client is going to be a class and the client class is going to use the Proxy class.
When to use the Proxy Design Pattern in C# Real-Time Applications?
Instead of allowing all users to access specific resources or functions of an object, we want to ensure that only certain users can access those resources or functions at any given time. We can achieve this functionality very easily using the Proxy Design Pattern in C#. The following are some of the real-time scenarios, where you can use the Proxy Design Pattern Real-time Applications.
- Adding security access to an existing object. The proxy will determine if the client can access the object or not.
- Simplifying the API of a complex object. The proxy can provide a simple API so that the client code does not have to deal with the complexity of the object of interest.
- Providing interfaces for remote resources such as web service or REST resources.
- Coordinating expensive operations on remote resources by asking the remote resources to start the operation as soon as possible before accessing the resources.
- Adding a thread-safe feature to an existing class without changing the existing class code.
In the next article, I am going to discuss the Real-Time Example of the Virtual Proxy Design Pattern in C#. Here, in this article, I try to explain the Protection Proxy Design Pattern in C# with Examples. I hope you enjoy this Protection Proxy Design Pattern in C# with Examples article.