Agent model of software design pattern (Java)

It seems that I've stopped writing for nearly half a month. I really shouldn't. in the new year, I always return to zero.

Back to zero, let's start this year with this article.

This article is going to introduce the basic concepts of agent mode, the advantages, disadvantages and usage of static agent and dynamic agent (including extension package cglib)

The agency model, also known as the entrustment model, entrusts something to do something as the name suggests.

For example, as the new year approaches, all the partners studying and working in other places also begin to buy train tickets home. Not to mention online booking, the partners close to the railway station can naturally go to the station to buy tickets directly. What should we do if they live in the suburbs far away from the railway station? Of course, you can take a special ride to the railway station to buy tickets, but it's really troublesome. At this time, a thing - the railway ticket agency is derived. Yes, with it, we small partners far away from the railway station can easily buy train tickets.

The train ticket agency here is actually an agent (entrustment). The train ticket agency sells tickets at the railway station. However, we can do some operations we want, such as authority control. The agency does not have the authority to refund tickets.

Well, back to business, let's take a look at the concept of agency

1. The proxy concept provides a proxy for an object to control access to that object. The proxy class and the proxied class have a common parent class or parent interface, so the proxy object can be used wherever the proxied class object is used. The proxy class is responsible for the preprocessing and filtering of the request, assigning the request to the proxy class for processing, and the subsequent processing after the proxy class executes the request.

A UML diagram:

As can be seen from the figure, the proxy interface (subject), proxy class (proxysubject) and proxied class (realsubject) form a "product" structure.

Both the proxy class and the proxy class implement the proxy interface at the same time, and then the proxy class processes the user's access request by referring to the object of the proxy class.

According to the generation time of the agent class, there are two kinds of agents, one is static agent and the other is dynamic agent.

The theoretical concept of wencrepe is uncomfortable. Here are some examples to help you understand.

2. Static proxy

The developer creates or a tool generates the source code of the agent class, and then compiles the agent class. The so-called static means that the bytecode file of the proxy class already exists before the program runs, and it is determined before the proxy class and the proxy class run.

Here, a timer operation is simulated to record the time of processing business. The following code is directly shown:

The first is the proxy interface, which is responsible for declaring business methods. Both proxy classes and proxy classes must implement it.

Next is the proxy class, which implements the proxy interface and performs the main business logic operations.

Then there is the proxy class, which also implements the proxy interface. In this way, the objects of the proxy class and the proxy class are "assimilated", and the objects of the proxy class can be referenced through the constructor, so as to access the main business methods of the proxy class.

Then a test class.

See the operation effect:

Here, the static agent has been implemented. Let's take a look at the advantages of the static agent. The proxy class only needs to pay attention to the main business implementation. Other operations such as logging, timing and permission control can be handed over to the agent class for additional processing.

However, is there really no problem with such an implementation?

If we want to implement timing functions for many business methods now, if these methods are distributed in different classes, do we need to create proxy classes for each class separately? In the long run, the class volume will expand and explode.

Let's take a step back. If we want to expand the business and add a new business method to the proxy interface, do all proxy classes need to add and implement this method in addition to the proxy class? The so-called "pulling one hair and moving the whole body" requires a lot of additional repeated code, This is against the principles of software design.

Also, proxy objects only serve one type of objects. If you want to serve multiple types of objects. It is bound to proxy for each object. Static proxy is incompetent when the program scale is a little large.

To solve this problem, our dynamic agent will appear.

3. JDK dynamic agent

The source code of JDK dynamic proxy class is dynamically generated by the virtual machine JVM according to reflection and other mechanisms during program operation, so there is no bytecode file of the proxy class. It is determined when the proxy class and the proxied class program run.

To implement the dynamic proxy mechanism in Java, you need Java lang.reflect. Invocationhandler interface and Java lang.reflect. Proxy class support.

Let's take a look at the introduction of the main methods in the Java API (refer to it for details)

java. lang.reflect. Proxy

java. lang.reflect. InvocationHandler

This is the calling processor interface. It defines an invoke method for centralized processing of method calls on dynamic proxy class objects. Usually, proxy access to the proxy class is realized in this method.

Dynamic agent implementation steps

1. Implement the invocationhandler interface to create its own call processor. 2. Provide the proxy class with classloader and proxy interface type array to create a dynamic proxy class. 3. Take the call processor type as the parameter and use the reflection mechanism to obtain the constructor of the dynamic proxy class. 4. Take the call processor object as the parameter and use the constructor of the dynamic proxy class to create a dynamic proxy class object

Well, let's go on. Some friends are going to be confused. Look at the examples directly and sort out their ideas

It is still the above proxy interface. Here, we need to create our own invocation processor (implement the invocationhandler interface)

Then comes the test class

Let's see the effect:

Advantages and disadvantages of dynamic agent: compared with static agent, The biggest advantage is that all the methods declared in the interface are transferred to a centralized method of the calling processor (invocationhandler. Invoke). In this way, when there are a large number of interface methods, we can handle them flexibly without transferring each method like a static agent. It can't be seen in this example because specific peripheral services are embedded in the invoke method (record the time before and after task processing and calculate the time difference). In practice, peripheral services can be configured like spring AOP. The application of dynamic agent makes our class responsibilities more single and reusable.

The fly in the ointment: proxy has been beautifully designed, but there is still a little regret, that is, it can not get rid of the shackles of only supporting interface proxy, because its design is doomed to this regret. JDK dynamic proxy can only proxy classes that implement interfaces. Classes that do not implement interfaces cannot proxy.

If there is a problem, you will want to solve it, so cglib will appear at this time.

4. Cglib dynamic proxy:

Introduction to cglib:

Agents provide a way to control the target objects to be accessed. When accessing objects, it introduces an indirect layer. JDK has introduced dynamic agents since version 1.3, and is often used to create agents dynamically. JDK's dynamic proxy is very simple to use, but it has a limitation that the object using dynamic proxy must implement one or more interfaces. What if you want to proxy an inherited class that does not implement an interface? Now we can use the cglib package.

Cglib implements proxy for classes. The implementation principle is to generate a subclass for the proxy class and intercept the calls of parent class methods through interception technology.

Let's take a small example:

The first is a business logic class

Another cglib proxy class, which provides proxy objects, needs to implement the methodinterceptor interface

Try writing a test class

Operation effect:

Author: Balla_ Rabbit origin: http://www.cnblogs.com/lichenwei/ The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original link must be given in an obvious position on the article page. I'm reading the children's shoes on my blog. I think you have extraordinary bearing. There's a king's spirit in your conversation. You must do something in the future! Next to the word "recommendation", you can easily point it. It's accurate. I won't accept a penny; If you're not sure, you can come back to me!

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
分享
二维码
< <上一篇
下一篇>>