Skip to main content

Proxy Design Pattern - C#

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

  1. public interface Subject
  2. {
  3. void PerformAction();
  4. }
  5.  
  6. public class RealSubject : Subject
  7. {
  8. public void PerformAction()
  9. {
  10. Console.WriteLine("RealSubject action performed.");
  11. }
  12. }
  13.  
  14. public class Proxy : Subject
  15. {
  16. private RealSubject _realSubject;
  17.  
  18. public void PerformAction()
  19. {
  20. if (_realSubject == null)
  21. _realSubject = new RealSubject();
  22.  
  23. _realSubject.PerformAction();
  24. }
  25. }

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

  1. /// <summary>
  2. /// The 'Subject interface
  3. /// </summary>
  4. public interface IClient
  5. {
  6. string GetData();
  7. }
  8.  
  9. /// <summary>
  10. /// The 'RealSubject' class
  11. /// </summary>
  12. public class RealClient : IClient
  13. {
  14. string Data;
  15. public RealClient()
  16. {
  17. Console.WriteLine("Real Client: Initialized");
  18. Data = "Dot Net Tricks";
  19. }
  20.  
  21. public string GetData()
  22. {
  23. return Data;
  24. }
  25. }
  26.  
  27. /// <summary>
  28. /// The 'Proxy Object' class
  29. /// </summary>
  30. public class ProxyClient : IClient
  31. {
  32. RealClient client = new RealClient();
  33. public ProxyClient()
  34. {
  35. Console.WriteLine("ProxyClient: Initialized");
  36. }
  37.  
  38. public string GetData()
  39. {
  40. return client.GetData();
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// Proxy Pattern Demo
  46. /// </summary>
  47. class Program
  48. {
  49. static void Main(string[] args)
  50. {
  51. ProxyClient proxy = new ProxyClient();
  52. Console.WriteLine("Data from Proxy Client = {0}", proxy.GetData());
  53.  
  54. Console.ReadKey();
  55. }
  56. }

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).

Comments

Popular posts from this blog

Accessing File Stored in Windows Azure Blob Storage Using jQuery

Did you know it was possible to access the Windows Azure Blob Storage directly from JavaScript, for example using jQuery? At first, it sounds obvious, since Blobs are after all accessible from a public UR. But in practice, there is a very big hurdle: the Web browser’s Same Origine Policy or SOP, that restricts JavaScript code to accessing resources originating from the same site the script was loaded from. This means that you will never be able to load a Windows Azure Blob using XMLHttpRequest for example! Fortunately, there is a popular workaround called JSONP (“JSON with Padding”). The idea behind this technique is that the script tag is not submitted to the SOP: an HTML page can thus load a JavaScript file from any site. So, if you expose your data in an “executable” form in JavaScript, a page will be able to load this data using a script tag. For example: <script type=”text/javascript” src=”http://www.sandeepknarware.in/exemple.jsonp”> </script> But how can ...

Support for debugging lambda expressions with Visual Studio 2015

Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitati...

gcAllowVeryLargeObjects Element

There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {    ...