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

Example Analysis of dynamic Agent in Java reflection Mechanism

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Java reflection mechanism in the dynamic agent example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

1. Agent mode

The proxy pattern is to provide a proxy for other objects to control access to that object. In fact, the proxy pattern is to introduce a certain degree of indirectness when accessing objects, which can be used for a variety of purposes.

Its characteristic is that the proxy class has the same interface as the delegate class, and the proxy class is mainly responsible for preprocessing messages, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class. the object of a proxy class is associated with the object of a delegate class, and the object of the proxy class itself does not really implement the service, but by calling the relevant methods of the object of the delegate class. to provide specific services.

two。 classification

According to the creation period, proxy class can be divided into two types, static proxy class and dynamic proxy class.

Static proxy class: created by a programmer or automatically generated by a specific tool, and then compiled. The .class file for the agent class already exists before the program runs.

Dynamic proxy class: dynamically created by using reflection mechanism when the program is running.

3. Examples of static and dynamic agents

Static proxy:

Business interface class:

Package com.bjpowernode.pattern; public interface UserManager {public void addUser (String userId, String userName); public void delUser (String userId); public void modifyUser (String userId, String userName); public String findUser (String userId);}

Business interface implementation class:

Package com.bjpowernode.pattern; public class UserManagerImpl implements UserManager {public void addUser (String userId, String userName) {/ / System.out.println ("start-- > > addUser () userId-- > >" + userId); try {System.out.println ("UserManagerImpl.addUser () userId-- > >" + userId); / / System.out.println ("success-- > > addUser ()") } catch (Exception e) {e.printStackTrace (); / / System.out.println ("error-- > > addUser ()"); throw new RuntimeException ();}} public void delUser (String userId) {System.out.println ("UserManagerImpl.delUser () userId-- > >" + userId) } public String findUser (String userId) {System.out.println ("UserManagerImpl.findUser () userId-- > >" + userId); return "Zhang San";} public void modifyUser (String userId, String userName) {System.out.println ("UserManagerImpl.modifyUser () userId-- > >" + userId);}}

Business agent class:

Package com.bjpowernode.pattern; public class UserManagerImplProxy implements UserManager {private UserManager userManager; public UserManagerImplProxy (UserManager userManager) {this.userManager = userManager;} public void addUser (String userId, String userName) {try {System.out.println ("start-- > > addUser () userId-- > >" + userId); userManager.addUser (userId, userName) System.out.println ("success-- > > addUser ()");} catch (Exception e) {e.printStackTrace (); System.out.println ("error-- > > addUser ()");}} public void delUser (String userId) {} public String findUser (String userId) {return null } public void modifyUser (String userId, String userName) {}}

Client class:

Package com.bjpowernode.pattern; public class Client {/ * * @ param args * / public static void main (String [] args) {/ / UserManager userManager = new UserManagerImpl (); UserManager userManager = new UserManagerImplProxy (new UserManagerImpl ()); userManager.addUser ("0001", "Zhang San");}}

Running result:

Start-- > addUser () userId-- > > 0001 UserManagerImpl.addUser () userId-- > > 0001 success-- > > addUser ()

Dynamic proxy:

Business interface class:

Package com.bjpowernode.pattern; public interface UserManager {public String test (String userId);}

Business interface implementation class:

Package com.bjpowernode.pattern; public class UserManagerImpl implements UserManager {public String test (String userId) {System.out.println ("UserManagerImpl.findUser () userId-- > >" + userId); return "Zhang San";}}

BusinessHandler class:

Package com.bjpowernode.pattern; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class BusinessHandler implements InvocationHandler {private Object targetObject; public Object newProxyInstance (Object targetObject) {this.targetObject = targetObject Return Proxy.newProxyInstance (targetObject.getClass (). GetClassLoader (), targetObject.getClass (). GetInterfaces (), this);} public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("start-- > >" + method.getName ()); for (int item0) I > test 0001 UserManagerImpl.findUser () userId-- > > 0001 success-- > > test Client.main ()-Zhang San thanks you for reading this article carefully. I hope the article "sample Analysis of dynamic agents in Java reflection Mechanism" shared by the editor will be helpful to everyone. At the same time, I also hope you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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