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 integrate factory design patterns to apply Annotation operations

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to integrate factory design patterns to apply Annotation operations". 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!

The overall role of Annotation is now clear, but what exactly can Annotation do in development? To further understand the processing purpose of Annotation, we will apply the Annotation operation in conjunction with the factory design pattern.

Import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class JavaAPIDemo {public static void main (String [] args) throws Exception {/ / IMessage msg = Factory.getInstance (MessageImpl.class); / / msg.send ("www.mldn.cn") MessageService messageService=new MessageService (); messageService.send ("www.mldn.cn") } @ Retention (RetentionPolicy.RUNTIME) @ interface UserMessage {public Class clazz ();} @ UserMessage (clazz = MessageImpl.class) / / use Annotation to implement the use of class MessageService {private IMessage message;public MessageService () {UserMessage use=MessageService.class.getAnnotation (UserMessage.class); this.message = (IMessage) Factory.getInstance (use.clazz ()); / / obtain} public void send (String msg) {this.msg.send (msg) directly through Annotation }} class Factory {private Factory () {} public static T getInstance (Class clazz) {/ / directly returns an instantiated object try {return (T) new MessageProxy (). Bind (clazz.getDeclaredConstructor (). NewInstance ());} catch (Exception e) {e.printStackTrace (); return null;} interface IMessage {public void send (String msg) } class MessageImpl implements IMessage {@ Overridepublic void send (String msg) {System.out.println ("[messaging]" + msg);}} class NetMessageImpl implements IMessage {@ Overridepublic void send (String msg) {System.out.println ("[network messaging]" + msg);}} class MessageProxy implements InvocationHandler {private Object target;public Object bind (Object target) {this.target = target Return Proxy.newProxyInstance (target.getClass (). GetClassLoader (), target.getClass (). GetInterfaces (), this);} public boolean connect () {System.out.println ("[proxy operation] makes a connection to the message sending channel.") ; return true;} public void close () {System.out.println ("[proxy operation] closes the connection channel.") ;} @ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {try {if (this.connect ()) {return method.invoke (this.target, args);} else {throw new Exception ("[ERROR] message cannot be sent!") Finally {this.close ();}

Execution result:

Replace

@ UserMessage (clazz = NetMessageImpl.class) "how to integrate factory design patterns to apply Annotation operations" ends here. 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