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:

  1. 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.
  2. 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:

  1. Remote Proxy: Provides a Proxy for objects in different address Spaces, either on the same host or on another host.
  2. 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.
  3. Protect Proxy: Also called firewall Proxy, it provides different levels of access rights for different users.
  4. Cache Proxy: Provides a Cache of an object so that multiple clients can share the object efficiently.
  5. Smart Reference Proxy: Provides additional operations when an object is referenced.

advantages

  1. Clients can program for abstract topics, add and replace proxy classes without modifying the source code, in line with the open closed principle.
  2. The coupling of the system is reduced by invoking real business through proxy objects.

In addition, different types of proxy patterns have unique advantages:

  1. 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.
  2. Virtual agents can save system overhead.
  3. Caching proxy improves system performance through caching.
  4. Secure agents enhance system security.

disadvantages

  1. Adding a proxy object between the client and the real topic can slow down requests and add complexity to the system.
  2. Implementing the proxy pattern requires additional work, and the proxy implementation process can be complex.

Application scenarios

  1. A remote proxy can be used when a client object needs to access an object in a remote host.
  2. 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.
  3. Caching proxies can be used when an object is invoked frequently and no recalculation is required for each invocation.
  4. You can use a protected proxy when you need to provide different levels of access for different users.
  5. 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.