[Java Study Notes] Detailed Explanation of Proxy Mode

I recommend everyone to watch the video of the power node , which is more helpful for understanding

Proxy mode

1. How to understand the proxy model

Understand the agent mode through Lao Du's method (analogous to the real scene), as follows:

image-20230330155306312

1. The three roles of proxy mode: target object, proxy object, public interface

filming Proxy mode
first role actor target
second role stuntman proxy object
third role Actors and stand-ins have the same action behavior Public interface of target object and proxy object

Here is the code to show:

image-20230330155242665

The relationship diagram of the three is as follows:

image-20230330160030692

  • The relationship between the three is that both the target object and the proxy object implement the same public interface

2. Three functions of the proxy mode

1) Protect yourself

  • In the program, when an object needs to be protected, you can consider using a proxy object to complete a certain behavior.
  • In real scenes, actors are afraid to let a stand-in actor (agent analysis) perform in order to protect themselves

2) Enhanced functions

  • In the program, when you need to enhance the function of an object , you can consider finding an agent to enhance it.
  • In real scenarios, for example, if we want to rent a house, we can rent it ourselves (target object), or we can find an intermediary group (proxy object), which realizes enhanced functions

3) Agent interaction

  • In the program, when the A object cannot directly interact with the B object, the proxy mode can also be used to solve it.
  • In the real scene, the man and woman did not meet before the blind date, and the matchmaker realized the communication between different objects by contacting the parents (proxy objects) of both parties.

3. Implementation of the proxy mode

Divided into static proxy and dynamic proxy

will be elaborated next

2. Static proxy

1. Explain in simple terms, starting from a common scenario in real business

Introduce the example of Lao Du:

A certain business module of a certain project has been running normally for many years, and the project manager requires these modules to be optimized. The first step is a simple requirement: to display the execution time of each module (method). For example, I have such a demand now, how should I realize it?

1) Method 1, hard coding, directly modify the original business code, and add a timing business code based on it.

image-20230330175917491

Analyze its advantages and disadvantages

  • Advantages: simple and easy to understand, hard coding is very rough
  • Disadvantages: Violates the OCP opening and closing principle , and the code is redundant

Insert a knowledge point, what is the OCP opening and closing principle?

The Open/Closed Principle (OCP) stipulates that "objects (classes, modules, functions, etc.) in software should be open for extension, but closed for modification. " Its meaning is that a software entity should implement changes through expansion, rather than through modification of existing codes.

This feature is especially valuable in a production environment, where changes to source code require code reviews, unit tests, and the like to ensure quality for use in production.

2) Method 2: Create a new class to inherit the original business, and rewrite the method of the parent class in the subclass to achieve business requirements

image-20230330180104593

Analysis of advantages and disadvantages

  • Advantages: Comply with the OCP opening and closing principle
  • Disadvantages: the code is as redundant, and the degree of coupling is increased (because of the inheritance relationship)

3) Introduce proxy mode, that is, use proxy objects to achieve function enhancement

As mentioned just now, the three roles required by the proxy mode: target object, proxy object, and public interface. [Target object + announcement interface] has been realized in normal business, and what is still missing is a proxy object.

Then the proxy object is very simple in the coding layer. Create an object and implement the announced interface. At the same time, when the client is required to use the proxy object, it is the same as using the target object !

So, how is the static proxy implemented in the code?
  1. First create a proxy object and implement the same public interface
  2. Rewrite the parent class method normally, and write the business logic required to enhance the function
  3. reference target object (how to reference?
  4. In the proxy object, call the corresponding method of the target object,
How to reference the target object? look at the code

image-20230330223532848

Insert knowledge points: the relationship between classes and the second [generalization relationship and association relationship]

  • The generalization relationship is inheritance, which is a relationship is aof
    • For example, an apple is a fruit and a cat is an animal. This is a generalization relationship.
  • An association relationship, that is, a reference, is a has arelationship of
    • For example, a person owns an apple, and a person owns a cat. This is an association relationship.

2. What are the advantages and disadvantages of the static proxy mode

Advantages :

  • Solved the OCP problem
  • The association relationship is used, the has a relationship is introduced, and the coupling is reduced

shortcoming

  • The code is still redundant. If there are too many interfaces in the system, and each interface has a proxy object, [ class explosion ] will occur

  • Not easy to maintain (too many classes are not conducive to modifying the business later

In order to overcome the shortcomings of the static proxy mode, a dynamic proxy is introduced

The technology of dynamically generating bytecode proxy classes in memory is called dynamic proxy. Dynamic proxy
is still proxy mode, but with the addition of bytecode generation technology , it can dynamically generate a cLass bytecode for us in memory. This word Section code is the proxy class.

3. Dynamic agent

1. What is a dynamic proxy?

As mentioned earlier, dynamic proxy is the technology of dynamically generating [bytecode proxy class] in [memory]. (Although there is no need for developers to write, the proxy object still exists at the memory level]

Advantages :

  • Reduced number of proxy classes
  • And solve the problem of code reuse.

The common implementation technologies of dynamic proxy include the following three

  • JDK's built-in dynamic proxy technology: only proxy interface
    • Location: java.lang.reflect.Porxy, is an annotation
  • CGLIB (Code Generation Library) dynamic proxy technology, an open source project, generates class libraries, which can be applied to interfaces and classes
    • But the lower layer of CGLIB is implemented through [inheritance] (although it is inheritance, but because the bytecode class is dynamically generated in memory, it will not increase the degree of coupling), so the performance is better than that of JDK dynamic proxy
    • The lower level of CGLIB also has a bytecode processing framework [ASM] (may be encountered when reading the source code)
  • Javassist Dynamic Proxy Technology: An open source project created by Professor Chiba Shigeru of the University of Tokyo. Implementation of the "aop" framework for JBOOS
    • The bottom layer of the mybatis framework is to use javassist to create the bytecode object of the interface

The lower layer of Spring is mainly realized by the dynamic proxy and CGLIB built in JDK

Guess you like

Origin blog.csdn.net/Xcong_Zhu/article/details/129869145