Agency Mode: Does Mr. Need Purchasing?

The term "agent" has become a household name in recent years. We can also understand the "agent model" through the logic of purchasing agents.

 

In this issue of dry goods article, let's talk about the "agent mode", one of the 23 classic modes of GOF in programming. Here we take the java language as an example to explain.

 

1. Proxy mode

 

Agent mode is a structural design mode , and one of the most commonly used design modes in development.

 

It is defined as follows: provide a proxy for other objects to control access to this object.

 

The proxy mode is mainly used to solve: some problems caused by direct access to the object, such as: we need to log a business method before and after execution, in order to achieve the purpose of decoupling, we need to create a proxy class to complete both Call the original business method, and log processing can be performed before and after the call.

 

Usage scenarios of the proxy mode: There are many in our development, such as the delayed loading of Spring containers, AOP enhanced processing, log processing, etc.

 

The role of the agent model:

 

 

Abstract role:  A business method that declares that the real role needs to be proxied through an interface or abstract class.

 

Delegated role (real role):  The object being delegated needs to implement the business method in the abstract role so that it can be delegated to the business method.

 

Agent role: The realization of abstract roles is an agent of real roles. By realizing abstract roles, enhanced functions and services can be added on the basis of real roles.

 

Agent model classification :

●Static proxy

●Dynamic Agent

 

2. Static proxy

 

The static proxy is the code of the proxy class created by the programmer or generated by the tool, and then compiled.

 

It is called a static proxy because the .class file of our proxy class already exists before the program runs, and the relationship and method enhancement between the proxy class and the delegate class (real role) have been determined before the program runs.

 

Let's take the example of adding log service mentioned earlier to see the implementation of static proxy:

 

Abstract role:

 

 

Delegation role:

 

 

Agent role:

 

 

Use of static proxy:

 

 

analysis:

 

The characteristic of static agent is to enhance the method without affecting the real role business method, and complete the decoupling of business and enhancement.

 

The disadvantage is that the proxy class and the delegate class implement the same abstract interface, which not only causes the code duplication-when we want to add a business method in the future, the proxy class and the delegate class code will also be changed significantly, and an agent Classes can only proxy for one type of object. If you want to proxy for other types of objects, you need to rewrite the set we wrote above.

 

In order to solve the shortcomings of static proxy, dynamic proxy mode appeared in java.

 

3. Dynamic Agent

 

Dynamic proxy is generated by the JVM through reflection mechanism or modified bytecode technology during the running of the program to generate proxy class objects. In the implementation phase, we don’t care about the proxy class, but only specify which object in the running phase, because the proxy class here is not What is written by the programmer is dynamically generated by the system, so it is called a dynamic agent.

 

There are two main ways to implement dynamic agents in Java:

● JDK dynamic proxy

JDK dynamic proxy is to use reflection mechanism to generate proxy objects during program operation. JDK dynamic proxy can only proxy classes that implement interfaces.

 

● CGLib dynamic agent

The CGLib dynamic proxy uses the asm open source package to load the class file of the proxy object class and process it by modifying its bytecode to generate subclasses. Can proxy classes that do not implement interfaces.

 

The implementation of these two dynamic agents is actually the two methods used in the classic Java framework spring.

 

Because JDK dynamic proxy is provided by JDK, we can use it directly, and CGLib is a third-party open source that needs to download and import third-party jar packages or dependencies, so here we mainly explain the implementation of JDK dynamic proxy.

 

JDK dynamic proxy implementation:

 

JDK dynamic proxy implementation steps:

(1) Create a class that implements the interface InvocationHandler;

(2) Invoke the newProxyInstance static method of Proxy in the custom InvocationHandler implementation class to create a proxy class.

(3) Reimplement the invoke method of the class and add enhanced functions.

(4) Call the target method through the proxy object.

 

Code:

Our abstract interface UserService and UserServiceImp remain unchanged for dragging down. Below we only need to complete the InvocationHandler implementation class.

 

 

Use of dynamic proxy:

 

 

analysis:

 

Compared with static proxy, the biggest advantage of dynamic proxy is that all methods declared in the interface are transferred to a centralized method of the invocation handler (InvocationHandler.invoke).

 

In this way, when the number of interface methods is relatively large, we can perform flexible processing, instead of needing to transfer each method like a static proxy. Moreover, the application of dynamic agents makes our class responsibilities more single and reusable.

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43802541/article/details/111396070