13
SepProxy Design Pattern
Proxy Design pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. The proxy design pattern is used to provide a surrogate object, which references to other objects. In this article, I would like to share what is a proxy pattern and how is it work?
What is Proxy Pattern
The proxy design pattern is used to provide a surrogate object, which references to other objects.
The proxy pattern involves a class, called proxy class, which represents the functionality of another class.
Proxy Pattern - UML Diagram & Implementation
The UML class diagram for the implementation of the proxy design pattern is given below:
The classes, interfaces, and objects in the above UML class diagram are as follows:
Subject
This is an interface having members that will be implemented by RealSubject and Proxy class.
RealSubject
This is a class which we want to use more efficiently by using proxy class.
Proxy
This is a class which holds the instance of RealSubject class and can access RealSubject class members as required.
C# - Implementation Code
public interface Subject { void PerformAction(); } public class RealSubject : Subject { public void PerformAction() { Console.WriteLine("RealSubject action performed."); } } public class Proxy : Subject { private RealSubject _realSubject; public void PerformAction() { if (_realSubject == null) _realSubject = new RealSubject(); _realSubject.PerformAction(); } }
Proxy Pattern - Example
Who is what?
The classes, interfaces, and objects in the above class diagram can be identified as follows:
IClient- Subject Interface.
RealClient - RealSubject Class.
ProxyClient - Proxy Class.
C# - Sample Code
/// <summary> /// The 'Subject interface /// </summary> public interface IClient { string GetData(); } /// <summary> /// The 'RealSubject' class /// </summary> public class RealClient : IClient { string Data; public RealClient() { Console.WriteLine("Real Client: Initialized"); Data = "Dot Net Tricks"; } public string GetData() { return Data; } } /// <summary> /// The 'Proxy Object' class /// </summary> public class ProxyClient : IClient { RealClient client = new RealClient(); public ProxyClient() { Console.WriteLine("ProxyClient: Initialized"); } public string GetData() { return client.GetData(); } } /// <summary> /// Proxy Pattern Demo /// </summary> class Program { static void Main(string[] args) { ProxyClient proxy = new ProxyClient(); Console.WriteLine("Data from Proxy Client = {0}", proxy.GetData()); Console.ReadKey(); } }
Proxy Pattern Demo - Output
There are various kinds of proxies, some of them are as follows:
Virtual proxies: Hand over the creation of an object to another object
Authentication proxies: Checks the access permissions for a request
Remote proxies: Encodes requests and send them across a network
Smart proxies: Change requests before sending them across a network
When to use it?
Objects need to be created on demand means when their operations are requested.
Access control for the original object is required.
Allow accessing a remote object by using a local object(it will refer to a remote object).
What do you think?
I hope you will enjoy the Proxy Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
FAQs
Q1. What is the main purpose of the Proxy pattern?
Q2. What problems does the Proxy pattern solve?
Q3. What are the variations of Proxy pattern?
- Virtual Proxy: Manages resource-intensive objects, producing them only as needed.
- Protection Proxy: Limits access to sensitive items by implementing security measures.
- Remote Proxy: Manages communication between items on various machines or networks.
- Caching Proxy: Stores results to reduce redundant operations and improve performance.