【Design Pattern Notes】Agent Pattern

The proxy pattern is the bottom layer of Spring AOP. The proxy pattern is the bottom layer of Spring AOP.

 

Static proxy:

Character Analysis:

Abstract roles: generally use interfaces or abstract classes to solve

Real role : the role that is being represented

Acting role : Acting as a real role, after acting as a real role, it will generally do some subsidiary operations

Client: The person accessing the proxy object

Advantages of proxy mode:

(1) It can make the operation of real characters more pure, without paying attention to some public services

(2) The public business is handed over to the agent role, realizing the division of labor

(3) When the public business is expanded, it is convenient for centralized management.

Disadvantages: A real role will generate a proxy role, and the development efficiency will become lower.

Solution: Dynamic proxy

Additional operations

client:

The client's direct contact is that the intermediary rents the landlord's house. 

Example: Known interfaces have methods to add, delete, modify and check

 Now want to print log when CRUD is called

Normally, print statements can be added directly to each method body

But in this way, the original code has been changed.

If you don't want to change the original code, you can use the proxy mode to add auxiliary functions 

 

 Understanding: without changing the original business, add some expansion functions to the business and use the agency model

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124460787