Problem
Need to control access to a type for performance, security or other reasons.
Client should not know if they were calling the real service or a proxy.
Proxy also gives us time to do necessary things before sending request to real service and before sending response back to client.(like logging, caching, encrypt/decrypt…)
Proxy has similar structure as the Decorator Pattern, but the intent is different, Decorator pattern is for adding extra funcationalities to the original class whereas Proxy is focusing on control the access to the object.
This is another implementation of the Proxy Pattern, it doesn’t have the interface so we need to compose the RealService object in Proxy Class. One thing to notice is that the RealService properties and methods need to be marked as virtual for the Proxy Class to override them.
Proxy Variants
- Virtual Proxy
- stand in for expensive to create objects
- Remote Proxy
- Hide the detail to work with remote data or services.
- Smart Proxy
- Performs additional actions when a resource is accessed
- Protective Proxy
- controls access to a sensitive resource by checking for whether or not the client is authorized to perform those operations.
Virtual Proxy
Stands in for an expensive-to-create object. Typically responsible for getting real object. UI placeholders. Lazy-loaded Entity Properties.
1 | public class ExpensiveToFullyLoad : BaseClassWithHistory |
When you have some expensive properties. You don’t want to create them when you don’t need them. So we could create a Proxy Class(VirtualExpensiveToFullyLoad), which will only create the property when its getting called.
1 | public class VirtualExpensiveToFullyLoad : ExpensiveToFullyLoad |
When we test the class, we can see object history will only increase after we get the Entities from the class.
1 | [Fact] |
We could also use the C# Lazy<T>
type which will handle the lazy instantiation and thread-safe for use
1 | public class LazyExpensiveToFullyLoad : BaseClassWithHistory |
Remote Proxy
Client works with proxy as if remote resource were local. Hides network details from client. Centralizes knowledge of network details.
Smart Proxy
Performs additional logic around resource access. Example: Resource counting, Cache management, Locking shared resources
Here we are trying to open the same file two times, normally this will throw an exception.
1 | var fs = new FileSmartProxy(); |
But we are using FileSmartProxy() Class, when we catch the exception, we will check if the file is already opened, and return the same reference to the file stream.
1 | public class FileSmartProxy : IFile |
Protective Proxy
Manages access to a resource based on authorization rules. Eliminates repetitive security checks from client code and othe resource itself. Acts as a gatekeeper around a resource
Summary
If we are not using the Proxy Pattern, we often end up mixing the concerns of access control, or lazy loading or other funcationality in the resource class itself. Every client the consume this class must perform this work. The concerns of access control are mixed with the concerns of client or the resource. Proxy Pattern helps us to separate this.
Usually Proxy Pattern has built in class that support it.(Remote Proxy)
Related Patterns
- Decorator: the structure is similar, but the intent of Decorator Pattern is to add funcationality. Whereas the intent of Proxy Pattern is to control access.
- Prototype: Prototype and Virtual Proxy Pattern both deal with objects that are expensive to create. But Virtual Proxy Pattern only provides a placeholder of the object and fetch it when required. The Prototype Pattern keeps a copy of the object on hand and can clone it when required.
- Adapter: similar structure, but the intent of the Adapter Pattern is to convert an incompatible interface into one that works for the client.
- Flyweight: designed to manage many reference to a shared instance.