Proxy is a structural design pattern: 🖤🖤🖤🤍🤍
What is Proxy?
Proxy is a design pattern that provides a surrogate or placeholder object for another object to control access to it. The proxy object has the same interface as the original object and delegates all the requests to it. The proxy can add some functionality or validation before or after accessing the original object.
A simple example of proxy is a company that restricts access to the free internet during working hours.
Why use Proxy?
Proxy is a design pattern that can be useful when you want to add some functionality or validation before accessing the original object.
- It can enhance the security and performance of the original object by adding caching, logging, authorization, etc.
- It can reduce the memory usage and loading time of the original object by creating it only when needed.
- It can simplify the interface of the original object by hiding some complex details.
- It can introduce some overhead and latency in the communication between the proxy and the original object.
- It can be difficult to maintain consistency between the proxy and the original object if they are not updated together.
Question: I hate the Proxy, what are the alternatives?
Answer: You can merge the proxy logic into the original object, which may simplify the code but also increase the coupling and reduce the flexibility of the object. Or you can try using AOP, which also allows you to modify the behavior of the object without changing its code.
When to use Proxy?
Question: When should I use Proxy?
Answer: You should use Proxy when you want to control or modify the behavior of another object without changing its code.
You have a class called
FreeInternetAccess that grants access to anyone who wants to browse the free internet.
You want to control when the access can be granted based on the current time. You expect to see the message “Sorry, access to the free internet is restricted during working hours!” when you try to access the free internet using the proxy during working hours.
How to implement Proxy?
You can skip Proxy and code normally. Or you can try AOP for fun. This code uses AOP to change a method’s behavior without changing its code. It defines an aspect function that limits free internet access by time. It uses aspectlib to apply the aspect function to a FreeInternetAccess object. The aspect function can run or skip the original method. But be careful, AOP may cause:
- Bugs or side effects
- Low readability and maintainability
- High complexity and overhead
It looks cool, but it seems it’s not very friendly, right? Let’s see if proxy is more understandable.
A proxy implementation would be to use the
CompanyProxyInternetAccess class as a wrapper for the
FreeInternetAccess class and add some logic to check the current time before granting access. This would enforce the company policy and prevent unauthorized access during working hours, but it would also allow access during non-working hours.
This code correlates and reflects the proxy design pattern by creating a proxy object that has the same interface as the original object and delegates all the requests to it. The proxy object also adds some functionality or validation before accessing the original object.
Proxy is a design pattern that can help you control or modify the behavior of another object without changing its code. It can also enhance the security and performance of the original object by adding caching, logging, authorization, etc.