This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
An overview of the
Proxy Pattern is an object structure Pattern that accesses target objects through Proxy objects.
The proxy pattern acts as an intermediary by introducing proxy objects to connect the client and target objects. Its role is to hide the content that the client cannot see and add additional services that the client needs. For example, Zhang SAN runs a kettle factory. Customers only need to entrust Zhang SAN to obtain the products they need, and then Zhang SAN will give the actual production order. The demand of the customer is the abstract theme, Zhang SAN plays the role of the agent theme, and the kettle is the real theme.
structure
- Subject (Abstract topic) : Typically an abstract class or interface that declares a common interface between a real topic and a proxy topic. Clients typically program against abstract topic roles.
- Proxy: Contains references to real topics, which can replace real topics and control the use of real topics at any time.
- RealSubject (RealSubject) : real business operations are implemented, and clients can indirectly invoke operations defined in real topics through proxy topics.
classification
According to different proxy objects, it can be divided into static proxy and dynamic proxy:
- Static proxy: The proxy class is created before the program is compiled and run, the method enhancement rules are written, and the rules are adapted to the methods of the specific class.
- Dynamic proxy: also known as JDK proxy, interface proxy. Proxy classes are not created in advance, but are created by reflection during program execution. Method rules are written, but the class methods to fit these rules are uncertain.
Proxy patterns can be divided into many categories based on their purpose and implementation:
- Remote Proxy: Provides a Proxy for objects in different address Spaces, either on the same host or on another host.
- Virtual Proxy: Creates an object that consumes less resources to represent an object that consumes more resources. Real objects can be created only when needed.
- Protect Proxy: Also called firewall Proxy, it provides different levels of access rights for different users.
- Cache Proxy: Provides a Cache of an object so that multiple clients can share the object efficiently.
- Smart Reference Proxy: Provides additional operations when an object is referenced.
advantages
- Clients can program for abstract topics, add and replace proxy classes without modifying the source code, in line with the open closed principle.
- The coupling of the system is reduced by invoking real business through proxy objects.
In addition, different types of proxy patterns have unique advantages:
- Remote agent can move some objects and operations that consume more resources to a computer with better performance and improve the overall operating efficiency of the system.
- Virtual agents can save system overhead.
- Caching proxy improves system performance through caching.
- Secure agents enhance system security.
disadvantages
- Adding a proxy object between the client and the real topic can slow down requests and add complexity to the system.
- Implementing the proxy pattern requires additional work, and the proxy implementation process can be complex.
Application scenarios
- A remote proxy can be used when a client object needs to access an object in a remote host.
- Virtual proxy can be used when an object that consumes less resources needs to be used to represent an object that consumes more resources, thus reducing system overhead and shortening running time.
- Caching proxies can be used when an object is invoked frequently and no recalculation is required for each invocation.
- You can use a protected proxy when you need to provide different levels of access for different users.
- Smart reference proxies can be used when you need to provide some additional operations for accessing an object.
Applications in the JDK
Java.lang.reflect.proxy uses the Proxy mode in the JDK, where it is the dynamic Proxy.