The Proxy Design Pattern is a structural design pattern. that provides a surrogate or placeholder for another object to control access to it.
This pattern comes in handy when we want to add an extra layer of control over the access to an object, such as lazy loading, access control, or logging. In C#, the Proxy Design Pattern is commonly used to create a surrogate object that represents another object.
We can also say that the Proxy is the object the client calls to access the real object behind the scene. Proxy means in place of or on behalf of. That means, In the Proxy Design Pattern, a class represents the functionality of another class.
Component of Proxy Design pattern
- Subject: This is an interface that defines the members that will 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: 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: This class holds a reference to the RealSubject class and can access RealSubjecr class members as required. It must implement the same interface as the RealSubject to use the two interchangeably. In our example, it is the SharedFolderProxy class.
- Client: This Client will be a class, and the client class will use the Proxy class.
Example in C#
Here's an example of how you can implement the Proxy Design Pattern in C#
Let's say that there is a third-party API to get Gold prices in the stock market. These prices are not available for the client application directly. The client application must use a proxy class to get the actual resources, as the main system, that provides this kind of data, cannot allow any third-party to access its database directly.
The main point here will be that, the proxy will fetch the current prices from the actual source, only if the client says yes or no. So our client code will be calling the proxy class and the proxy class will get the price from the actual source and send the current price, back to the client code.
Let's convert the preceding real-world example into technical code.
namespace ProxyPattern.Subject { /// <summary> /// ISubject interface /// </summary> public interface IPrice { double GetPrice(); } }Creating Real Subject(GoldPrice.cs)
using ProxyPattern.Subject; namespace ProxyPattern.RealSubject { /// <summary> /// RealSubject class that implements the IPrice interface /// </summary> public class GoldPrice : IPrice { int minimum = 999; int maximum = 9999; public double GetPrice() { Random rnd = new Random(); return rnd.NextDouble() * (maximum - minimum) + minimum; } } }Creating Proxy Class(Proxy.cs)
using ProxyPattern.RealSubject; using ProxyPattern.Subject; namespace ProxyPattern.Proxy { /// <summary> /// Proxy Class /// </summary> public class Proxy { private IPrice _price; public double GetCurrentGoldPrice(bool isCurrentPrice) { if (isCurrentPrice) { _price = new GoldPrice(); // Get the actual price return _price.GetPrice(); } return 0; } } }CLient(Program.cs)
Now we need to write our client code, that will simply create a proxy class instance and call its function.
using ProxyPattern.Proxy; using ProxyPattern.RealSubject; // Initiate the proxy class Proxy _proxy = new Proxy(); // Call the proxy function var goldPrice = _proxy.GetCurrentGoldPrice(true); if (goldPrice > 0) Console.WriteLine("Current Gold price is :{0}", goldPrice); else Console.WriteLine("No call to Api"); Console.WriteLine(Environment.NewLine); goldPrice = _proxy.GetCurrentGoldPrice(false); if (goldPrice > 0) Console.WriteLine("Current Gold price is :{0}", goldPrice); else Console.WriteLine("No call to Api"); Console.ReadLine();
So as we see here, it is the proxy that is calling the actual resource. Various kinds of business logic like whether the call is from an authenticated source or not, or any other logic can be added to the proxy layer and control the way the resource is being accessed.
Output
A Proxy Design Pattern has various variations that include:
- 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 controls access to a sensitive master object. The surrogate object checks that the caller has the access permissions required before forwarding the request.
So depending on our requirements, we may have a combination of one or more of these variations like a protection proxy can be added along with the virtual and remote proxy to authenticate the client making a request for the required data. This was all about the Proxy Design Pattern.
Happy coding!! 😊
No comments:
Post a Comment