Agent Mode
The proxy model is called Proxy or Surrogate in English, and both can be translated as "agent" in Chinese. The so-called proxy means that one person or an institution takes action on behalf of another person or another institution. In some cases, a client does not want or cannot directly refer to an object, and the proxy object can act as an intermediary between the client and the target object
A. Abstract theme role
Declares a common interface between real and proxy topics, so that any real topic can be used can be used.
B. Proxy role:
The agent theme role contains references to real topics, so that real topic objects can be operated at any time; the agent theme role provides the same interface as the real topic role, so that it can replace the real topic to control references to real topics at any time, and is responsible for creating real topic objects (and deleting real topic objects) when needed; the agent role usually performs an operation before or after passing the client call to the real topic, rather than simply passing the call to the real topic object.
C. Real theme role
Defines the real object represented by the proxy role
For example:
Refactoring with dynamic proxy mode
JDK dynamic proxy
Target class interceptor proxy class
Summary: 1. Because the proxy class generated by JDKProxy implements the interface, all methods in the target class are included in the proxy class.
2. All methods of the generated proxy class intercept all methods of the target class. The content of the invoke method in the interceptor is exactly the composition of each method of the proxy class.
3. Interfaces must exist when using JDKProxy.
4. The three parameters in the invoke method can access the API of the called method, the parameters of the called method, and the return type of the called method of the target class.
Refactoring with JDK dynamic proxy mode
Reconstructing hibernate transactions using JDK dynamic proxy mode
cglib dynamic proxy
1. CGlib is a powerful, high-performance, and high-quality Code generation class library. It can extend Java classes and implement Java interfaces during runtime.
2. Use CGlib to generate a proxy class as a subclass of the target class.
3. No interface is required to generate proxy classes using CGlib
4. The proxy class generated by CGLib overrides the methods of the parent class.
5. The content of the intercept method in the interceptor is exactly the method body in the proxy class.
Refactoring hibernate transactions using cglib dynamic proxy mode
Spring two proxy methods
1. If the target object implements several interfaces, spring uses JDK's java.lang.reflect.Proxy class proxy.
Advantages: Because there are interfaces, the system is more loosely coupled. Disadvantages: Create interfaces for each target class.
2. If the target object does not implement any interface, spring uses the CGLIB library to generate a subclass of the target object.
Advantages: Because the proxy class and the target class are inherited, there is no need for an interface to exist.
Disadvantages: Because there is no interface used, the coupling of the system is not as good as the dynamic proxy using JDK.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.