A guide to software design patterns
Structural Design Pattern: Proxy
The proxy pattern, one of the seven structural patterns, is pretty much a class that represents another class. In plain English, a proxy is a wrapper class that provides the same and sometimes additional functionalities on top of the real object itself. The primary purpose of the proxy pattern is to control access to the real object.
When you call your phone service provider, for example, you usually speak to a proxy — i.e. a representative of the company. Chances are you will need to provide some kind of identifiable information, otherwise, you won’t be serviced. Your identity has nothing to do with the available services, it only determines if you are indeed a customer and allows you access to those services if you are eligible. The role of the representative (proxy) is to make sure you are a legitimate customer. The same can be said for a proxy class concerning the real class.
A proxy can be used to interface with many things such as caching for resource-intensive operations, validations, etc… When using a proxy, you still get access to all functionalities of the real object. A proxy should be used when you want to give access to the real object conditionally (e.g only authenticated and authorized users can access certain functionalities).
Let’s say we have a simple Surgery class with just one method named operate. You can never be too careful and always have to do your due diligence, there you have to make sure all the surgeons are board-certified and can operate on patients. What would that look like in code using the proxy pattern?
Let’s start with an interface name Surgery. The operate method takes the surgeon’s id as a parameter and returns true or false based on the outcome of the operation.
public interface Surgery {
boolean operate(String surgeonId);
}We also need a concrete implementation of the surgery interface. We’ll call it SurgeryImpl. In our case, all operations will be successful. Here’s the implementation of that class.
public class SurgeryImpl implements Surgery{
@Override
public boolean operate(String surgeonId) {
System.out.println("Operating in "
+ this.getClass().getSimpleName());
return true;
}
}Let’s now create the proxy class. It will extend the SurgeryImpl class, override the operate method and add validate the surgeon identity. We’ll only use the real object once we know our surgeon is board certified. Here’s the implementation for that class.
public class ProxySurgery extends SurgeryImpl{
@Override
public boolean operate(String surgeonId){
if(!isBoardCertified(surgeonId)){
return false;
}
return super.operate(surgeonId);
}
private boolean isBoardCertified(String surgeonId){
//validate surgeon id
System.out.println("validating in: " +
this.getClass().getSimpleName());
return true;
}
}I’ve also seen other implementation of this pattern where the proxy implements the real object interface, then creating an instance of the concrete class to access the real object functionalities.
A client can use the proxy and we now have validation in place that the real class didn’t provide. That will stop us from attempting to use the real object functionalities unless we know for sure we’re dealing with a certified surgeon. In the real world, the operate method might have to do database lookup and call other services, external services such as a third party. We want to make sure to prevent all that execution unless we know it’s all good on that front.
A client can use the proxy class like this:
public static void main(String[] args) {
ProxySurgery proxySurgery = new ProxySurgery();
boolean isSuccess = proxySurgery.operate("abc123");
System.out.println("Successful operation: " + isSuccess);
}The output looks like this:
Validating in ProxySurgery
Operating in ProxySurgery
Successful operation: trueIf you look closely, you’ll notice that the client has no knowledge of the real object because we use the operate method from the real class, however, the client’s visibility stops at the proxy.
This is a very simple use case implementation of the proxy pattern. In a more complex scenario, you would expose the validation method so that a client can validate a surgeon’s credibility before attempting to operate. The validation method would also return a failure message or code that indicates the reason for failure.
This is, in a nutshell, the proxy pattern. Thank you for making it to the end. Happy coding.
Previous: Structural Design Pattern: Decorator





