Proxy mode of Java design pattern notes

Proxy mode:

The proxy pattern is the structure pattern of objects. Proxy mode provides a proxy object for an object, and the proxy object controls the reference to the original object.

Core role:

Control access to objects through agents.

You can control in detail the method of accessing an object (a class), pre-processing before calling this method and post-processing after calling this method, that is, the micro implementation of AOP.

The core implementation mechanism of AOP (aspect oriented programming).

Scenes in life:

The so-called agency is that a person or institution takes action on behalf of another person or institution. In some cases, a client does not want or cannot directly reference an object, and the proxy object can act as an intermediary between the client and the target object. For example, if a customer wants to find a star to sing, he first needs to find his agent, and then his agent will arrange a star to sing. Before the concert, the agent needs to deal with some pre-processing (interview, contract drafting, signing, receiving advance payment, arranging air tickets and vehicles, etc.) and after the concert, some post-processing (closing payment, etc.). At this time, a star (real role) only needs to care about how to sing, and all other things are left to the agent (agent).

Core role:

Write a picture description here

Abstract object role: it declares the common interface between proxy object and real object, and defines the public external methods of proxy object and real object. This allows you to use proxy objects wherever you can use real objects.

Real object role: defines the real object represented by the proxy object. Implement abstract objects and define the business logic required by real objects for proxy objects to call. Focus on real business logic.

Proxy object role: implement the abstract object, which is the proxy of the real object. Implement the abstract method through the business logic method of the real object, and attach your own operations. Put the unified process control into the proxy object for processing.

The proxy object contains a reference to the real object, so that the real object can be operated at any time; Proxy objects provide the same interface as real objects so that they can replace real objects at any time. Proxy objects usually perform an operation before or after the client call is passed to the real object, rather than simply passing the call to the real object.

Application scenario:

Security proxy: block direct access to real roles. Remote proxy: handle remote method calls through proxy classes. Delayed loading: first load the lightweight proxy object, and then load the real object. (delayed loading of pictures)

Classification:

Static proxy: (statically define proxy class)

Code of the above example:

1. The common interface between proxy object and real object is declared, and the public external methods of proxy object and real object are defined.

2. Define a real object class and implement the methods provided by the abstract interface.

3. Define a class of proxy object, implement the methods provided by the abstract interface, and hold the reference of the real object.

4. Testing

The operation results are as follows:

As can be seen from the above example, the proxy object delegates the client's call to the real object, and can perform specific operations before and after calling the method of the target object.   

Dynamic proxy: (dynamically generate proxy class):

Advantages of dynamic agents over static agents:

All methods declared in the abstract role (Interface) are transferred to a centralized method of the calling server, so that we can deal with many methods more flexibly and uniformly

JDK's own dynamic agent

java. lang.reflect. Proxy dynamically generates proxy classes and objects

java. lang.reflect. Invocationhandler (processor interface) can realize proxy access to real roles through the invoke method

Each time a proxy class object is generated through proxy, the corresponding processor object must be specified

The test code is as follows:

1. The common interface between proxy object and real object is declared, and the public external methods of proxy object and real object are defined.

2. Define a real object class and implement the methods provided by the abstract interface.

3. Define a starhandler class to implement the invocationhandler processor interface. You can realize proxy access to real roles through the invoke method, or handle many operations uniformly in the invoke method.

4. Client test class

The operation results are as follows:

Before the real method is executed! Interview, contract signing, advance payment, air ticket booking, realstar Sing() after the real method is executed! Closing payment

Application scenarios in the development framework

There are many application scenarios of agent mode in the development framework. In fact, it is useful to select any development framework. For example:

Implementation of interceptor plug-in AspectJ in mybatis implementation of AOP in spring

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>