Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Java agent mode

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to understand the Java agent mode". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Static proxy pattern 1.1, definition of proxy pattern:

For some reason, you need to provide a proxy to an object to control access to that object. At this time, the access object is not suitable or cannot directly reference the target object, and the proxy object acts as an intermediary between the access object and the target object.

For example, in some cases, when a customer cannot or does not want to access another object directly, you need to find an intermediary to help complete a task, which is the proxy object. For example, you don't have to go to the railway station to buy a train ticket, you can buy it through the 12306 website or at a train ticket agency. For example, finding a girlfriend, looking for a babysitter, looking for a job, etc., can all be done by looking for an intermediary.

Static proxy: a programmer creates a proxy class or a specific tool automatically generates source code and compiles it, and the .class file of the proxy class already exists before the program runs.

Code example: to achieve the addition, deletion, modification and search operation, through the agent

Interface:

Package com.proxyPattern.staticProxy2;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className UserService * @ date 17:54 on 2021-12-27 * @ Description service interface * / public interface UserService {void add (); void delete (); void update (); void query ();}

Real class (here is the service class)

Package com.proxyPattern.staticProxy2;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className UserServiceImp * @ date 17:55 on 2021-12-27 * @ Description service implementation class * / public class UserServiceImp implements UserService {@ Override public void add () {System.out.println ("added a piece of data") @ Override public void delete () {System.out.println ("deleted a piece of data");} @ Override public void update () {System.out.println ("modified a piece of data");} @ Override public void query () {System.out.println ("query a piece of data");}}

Proxy class

Package com.proxyPattern.staticProxy2;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className UserServiceProxy * @ date 17:56 on 2021-12-27 * @ Description service proxy class * / public class UserServiceProxy implements UserService {private UserServiceImp userServiceImp; public UserServiceProxy () {} public void setUserServiceImp (UserServiceImp userServiceImp) {this.userServiceImp = userServiceImp;} @ Override public void add () {getLog ("add") UserServiceImp.add ();} @ Override public void delete () {getLog ("delete"); userServiceImp.delete ();} @ Override public void update () {getLog ("update"); userServiceImp.update ();} @ Override public void query () {getLog ("add"); userServiceImp.query () } public void getLog (String message) {System.out.println ("log:" + message + "statement executed");}}

Client test class

Package com.proxyPattern.staticProxy2;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className Customer * @ date 18:00 on 2021-12-27 * @ Description client terminal test class * / public class Customer {public static void main (String [] args) {UserServiceImp userServiceImp = new UserServiceImp (); UserServiceProxy p = new UserServiceProxy (); p.setUserServiceImp (userServiceImp); p.add () P.update (); p.delete (); p.query () }} / * execution result: * add statement executed * added a piece of data * log: update statement executed * modified a piece of data * log: delete statement executed * deleted a piece of data * log: add statement executed * queried a piece of data * /

The above code shows that we do not use userServiceImp to execute the method, but use a proxy class to execute, which is the proxy mode, which is similar to the fact that you do not rent a house from the landlord, but find an intermediary agent to complete the action.

1.2. Advantages and disadvantages of agent mode.

So what are the advantages of the agent model?

1. It can make our real characters more pure and stop paying attention to some public things.

2. The public business is done by the agent. Realize the division of business.

3. When the public business is expanded, it becomes more centralized and convenient.

Shortcoming

1. The agent mode will increase the number of classes in the system design.

2. Adding a proxy object between the client and the target object will slow down the processing of requests.

3. Increase the complexity of the system.

How to solve these problems? Rely on the following dynamic agent mode to solve the problem

Second, dynamic agent mode

Dynamic means that it is dynamically created by using the reflection mechanism when the program is running.

Yes, the dynamic proxy pattern uses reflection, and you have to write a dynamic proxy class to dynamically obtain a proxy class.

Code example: the case is the same as above, except that the dynamic proxy mode is adopted.

Service implementation class (real class)

Package com.proxyPattern.staticProxy2;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className Customer * @ date 18:00 on 2021-12-27 * @ Description client terminal test class * / public class Customer {public static void main (String [] args) {UserServiceImp userServiceImp = new UserServiceImp (); UserServiceProxy p = new UserServiceProxy (); p.setUserServiceImp (userServiceImp); p.add () P.update (); p.delete (); p.query () }} / * execution result: * add statement executed * added a piece of data * log: update statement executed * modified a piece of data * log: delete statement executed * deleted a piece of data * log: add statement executed * queried a piece of data * /

Interface:

Package com.proxyPattern. AutoProxy;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className UserService * @ date 17:54 on 2021-12-27 * @ Description service interface * / public interface UserService {void add (); void delete (); void update (); void query ();}

Dynamic proxy class, which can almost be used as a utility class, because the format is fixed

Package com.proxyPattern.autoProxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.autoProxy * @ className ProxyInvocationHandler * @ date 2021-12-27 19:33 * @ Description dynamic proxy class * / public class ProxyInvocationHandler implements InvocationHandler {/ / proxied interface private Object target; public void setTarget (Object target) {this.target = target } / * * @ Date 19:36 on 2021-12-27 * @ Param * @ Return Object * @ MetodName getProxy * @ Author wang * @ Description generate the proxy class * / public Object getProxy () {return Proxy.newProxyInstance (this.getClass (). GetClassLoader (), target.getClass () .getInterfaces (), this) } / * @ Date 19:34 on 2021-12-27 * @ Param * @ param proxy * @ param method * @ param args * @ Return Object * @ MetodName invoke * @ Author wang * @ Description processes the proxy instance and returns the result * / @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {log (method.getName ()) Object result = method.invoke (target, args); return result;} public void log (String message) {System.out.println ("log:" + message + "statement executed");}}

Client test class:

Package com.proxyPattern.autoProxy;/** * @ author wang * @ version 1.0 * @ packageName com.proxyPattern.staticProxy2 * @ className Customer * @ date 18:00 on 2021-12-27 * @ Description client terminal test class * / public class Customer {public static void main (String [] args) {/ / Real role UserService userService = new UserServiceImp (); / / Agent role ProxyInvocationHandler pih = new ProxyInvocationHandler () / / dynamically set proxy object pih.setTarget (userService); / / dynamically generate proxy class UserService proxy = (UserService) pih.getProxy (); proxy.query (); proxy.update ();}} / * * log: query statement executed * queried a piece of data * log: update statement executed * modified a piece of data * /

We can see that it is more convenient to get the proxy class here, as long as you change the object where you dynamically set the proxy class, you can proxy other classes.

This is the end of the content of "how to understand the Java Agent Mode". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report