Java design pattern – proxy pattern (example explanation)

Proxy pattern is one of the most common design patterns in Java. Spring's AOP uses the proxy mode.

Generally speaking, the agent mode is divided into static agent and dynamic agent.

As a design pattern of structural class, it is used to expand the class without modifying the internal code of the class. It is a supplement to the inheritance mechanism.

Eg: let's implement the proxy mode for the example of user login.

The basic requirement is to realize the function of user login and modifying nickname.

First, iuser interface and user implementation class

Client class

It's still very simple. But later, the product manager told you that we need to add a function to record user behavior. What should we do now? Modify user class directly? No, no, no, use proxy mode.

Add an agent class and write the function of "recording user behavior" in the agent class. Do not modify the class, but only expand the class to reduce errors.

Client class:

In this way, you only need to modify the client class and add a static proxy, which is a perfect implementation. But the demand is endless. The product manager says to you, "we have added an administrator role and a lot of secondary Administrators",

This is embarrassing. Each role has to build a static proxy class. Class explosion. No hurry, we have dynamic agent mode.

The dynamic proxy mode is that you don't need to create your own proxy class. If you pass the specific implementation class (principal) to him, he will generate a proxy class for you by default.

In essence, it uses the reflection mechanism of Java to dynamically generate the corresponding proxy class at runtime.

Without reflection, there is no dynamic proxy.

Last client class:

Admin class added due to demand

Summary:

1. The static proxy mode is relatively simple. The key point is to create a new proxy class for each implementation class (subject body). The proxy class contains references to the entity class (subject body), so that the original implementation class (subject body) can be controlled, including AOP control.

2. Static proxy has limitations. For each entity class, it may be necessary to create a new static proxy class, which may cause too many static proxy classes, so dynamic proxy came into being.

3. The dynamic agent is not limited to the specific implementation class (subject body). Internally, it uses object to access the reference of the entity class, and then uses reflection to obtain various methods of the entity class, so as to realize the aspect oriented AOP programming control of the implementation class (subject body).

4. The above description is a dynamic proxy in JDK, which is not particularly perfect, because this dynamic proxy requires an entity class to implement at least one interface. The problem is that not all classes have interfaces, so it's not perfect here.

The above is my own understanding of the agency model. If there are mistakes and omissions, please criticize and correct them. Thank you.

The above Java design pattern - proxy pattern (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>