Design patterns in C# - The Proxy Pattern
Continuing my series on design patterns, this week we're focusing on the proxy pattern!
The proxy pattern is used to provide access to an object. It is often used to enable this access over some distance - this could be providing remote access, or adding an extra level of protection around the object. The crucial thing is that the proxy pattern offers a way to indirectly provide (and control) access. It is similar in implementation to the decorator pattern, but its purpose is different. Both patterns wrap an inner object, however the decorator pattern expands on the functionality of the inner object, where the proxy pattern instead governs the access to the object.
The proxy pattern can be used to restrict access to an object, to provide a simpler or lightweight interface, or to allow the client to communicate with a remote object via a local representation.
Now, if we look at an example...
Proxy Example
So, say we have an INest
:
public interface INest
{
void Access(string name);
}
which is implemented by a RealNest
:
public class RealNest : INest
{
public void Access(string name)
{
Console.WriteLine($"{name} has access to the nest");
}
}
Now, we don't want just anyone to be able to access our Nest
. We need to be able to restrict access to only those that aren't going to cause harm. To do this, we can implement a SecureNestProxy
:
public class SecureNestProxy : INest
{
private readonly INest nest;
public SecureNestProxy(INest nest)
{
this.nest = nest;
}
public void Access(string name)
{
if (name == "TRex" || name == "Gigantosaurus")
{
throw new UnauthorizedAccessException($"{name} is not allowed to access the nest.");
}
else
{
this.nest.Access(name);
}
}
}
This Proxy
will reject access by dinosaurs we know to be carnivorous. If we then run the following:
var secureNestProxy = new SecureNestProxy(new RealNest());
secureNestProxy.Access("Stegosaurus");
secureNestProxy.Access("TRex");
Then the output will be the following:
Stegosaurus has access to the nest
Unhandled Exception: System.UnauthorizedAccessException: TRex is not allowed to access the nest.
And we can see that if we wrap our underlying objects in our proxy, we can restrict access as we need.
Other examples of uses for this pattern include caching (where the requested data doesn't need to be repeatedly requested from the service, and a copy is instead stored in memory inside the proxy), for lazy loading of large objects, or to locally represent objects which exist outside of your system.
Thanks for reading! Here's a link to the GitHub repository which contains all the code for this blog series. And watch of for my next blog on design patterns in C#!