- Name Proxy
- Type Structural Design Pattern
- Description An object acts as a placeholder for another object.Client interacts with Proxy(placeholder) rather then actual object.
In the Proxy Design pattern an object called the Proxy is used as a placeholder for another object.The main object provides the actual functionality whereas the proxy object is used just a stand-in object for the real object.Client interacts only with the proxy object instead of the real object.Here we will see the use and example of Proxy Design Pattern.
In Proxy design pattern ,when a request is received by the Proxy object ,it delegates the request to the original object.
Client uses the Proxy object in the similar way it uses the Real object as both implements a common interface.
Use of Proxy Design Pattern
There are many common scenarios in which Proxy pattern is useful:
To simplify use of an object
Perhaps the most common example where Proxy pattern is used is when we create a proxy of a web service.The application uses the Proxy object just as it would use the real object.
Application is not aware that the proxy is calling the web service which can be located anywhere.Web Service could be hosted in a process on the same machine or on a machine in a different country .Client uses the proxy just as it would use a real web service.This is also a example of remote proxy as the proxy object is used to access a remote web service.
In the case of a Remote Proxy different communication mechanisms such as remoting or web services might be used.So instead of exposing these communication details to the client a proxy is used instead.
Protection Proxy
If there is an object which is sensitive then instead of directly exposing the object in our application we can use proxy in our application.So our application is not aware of the existence of the real object and it interacts only with the proxy.The proxy can define the logic to verify the access to the real object.Only if the client is authorized a real object is created.
Virtual Proxy
Here the proxy object is used as a placeholder for an expensive object.Instead of creating the real object when the application is initialized ,a proxy object is created.Proxy object determines when the real object is actually required and only then creates the real object.This reduces the use of the resources ,such as database, until it is actually required.
Implementation of Proxy Design Pattern
We can represent the Proxy Design Pattern with the following UML diagram:
The three main participants in this pattern are:
Proxy Implements the Subject interface.Can be used in place of the RealSubject as both implements the same interface
RealSubject This is the object which provides the actual functionality.It Implements the Subject interface.
Subject Interface which is implemented by both the Proxy and the RealSubject.
Example of Proxy design pattern in C#
We are taking a real scenario of an Organization where each employee has a manager.An employee can have a specific manager.There is some confidential data for every employee which only the assigned manager of the employee should be able to see.
The data here is represented by the IConfidentialInfo interface.This interface defines the operations GetSalary and GetRating which returns the salary and rating of the employee.
Manager of an employee is represented by the Manager class.
Since salary and rating are confidential information of an employee so we are creating a proxy class for Maanger called ManagerProxy.This class contains the logic to verify if the Manger calling the GetSalary and GetRating methods is indeed the designated Manager of an Employee.If any other Manager tries to call these methods we throw UnauthorizedAccessException.
interface IConfidentialInfo { decimal GetSalary(int EmployeeId); int GetRating(int EmployeeId); } public class Manager : IConfidentialInfo { public int Id { get; set; } public string Name { get; set; } public Manager(string name, int id,List<Employee> reportees) { Name = name; Id = id; Reportees = reportees; } private List<Employee> _reportees; public List<Employee> Reportees { get; set; } public decimal GetSalary(int EmployeeId) { decimal salary = Reportees.Where(x => x.Id == EmployeeId).Select(x => x.Salary).FirstOrDefault(); return salary; } public int GetRating(int EmployeeId) { int rating = Reportees.Where(x => x.Id == EmployeeId).Select(x => x.Rating).FirstOrDefault(); return rating; } } public class ManagerProxy : IConfidentialInfo { Manager mgr; public List<Employee> Reportees { get; set; } public ManagerProxy(string name, int id, List<Employee> reportees) { mgr = new Manager(name, id,reportees); } public decimal GetSalary(int EmployeeId) { Employee emp = mgr.Reportees.Where(x => x.Id == EmployeeId).Select(x => x).FirstOrDefault(); if (emp != null) { return emp.Salary; } else { throw new UnauthorizedAccessException(); } } public int GetRating(int EmployeeId) { Employee emp = mgr.Reportees.Where(x => x.Id == EmployeeId).Select(x => x).FirstOrDefault(); if (emp != null) { return emp.Rating; } else { throw new UnauthorizedAccessException(); } } public class Employee { public string Name { get; set; } public int Id { get; set; } public decimal Salary { get; set; } public int Rating { get; set; } public Employee(string name, int id, decimal salary, int rating) { Name = name; Id = id; Salary = salary; Rating = rating; } } }
To test the ManagerProxy class we can create instance of the ManagerProxy class and pass different values for employeeIds
static void Main(string[] args) { List<Employee> Reportees=new List<Employee>{new Employee("Amit", 1, 10000, 2), new Employee("Mark", 2, 7000, 1)}; ManagerProxy manager = new ManagerProxy("Marc",32,Reportees); int employeeId=2; decimal salary= manager.GetSalary(employeeId); Console.WriteLine("Salary of Employee with Id {0} is {1}", employeeId, salary); Console.ReadLine(); }
If we execute the above code we will get the salary of the employee with id 2:
While if we now set the value of employeeId other than 1 or 2 which are valid employee ids for the managers reportees we will get unauthorized exception:
Here we have seen the Use and example of Proxy Design Pattern.We tried to understand when the proxy pattern is useful and how to implement the proxy design pattern in C#.
Leave a Reply