Please enable Javascript to correctly display the contents on Dot Net Tricks!

Proxy Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 14 Jul 2013
Total Views : 50,539   
Updated On : 26 Sep 2016
 

Proxy 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 object. In this article, I would like share what is 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 object.

Proxy pattern involves a class, called proxy class, which represents 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:

  1. Subject

    This is an interface having members that will be implemented by RealSubject and Proxy class.

  2. RealSubject

    This is a class which we want to use more efficiently by using proxy class.

  3. 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:

  1. IClient- Subject Interface.

  2. RealClient - RealSubject Class.

  3. 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:

  1. Virtual proxies : Hand over the creation of an object to another object

  2. Authentication proxies : Checks the access permissions for a request

  3. Remote proxies : Encodes requests and send them across a network

  4. Smart proxies : Change requests before sending them across a network

When to use it?

  1. Objects need to be created on demand means when their operations are requested.

  2. Access control for the original object is required.

  3. Allow to access 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.



Free Interview Books
 
COMMENTS (0)
2 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

Know More
27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

Know More
25 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
25 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM Weekend

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+