本篇內(nèi)容介紹了“如何整合工廠設(shè)計(jì)模式來(lái)應(yīng)用Annotation操作”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),日照企業(yè)網(wǎng)站建設(shè),日照品牌網(wǎng)站建設(shè),網(wǎng)站定制,日照網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,日照網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
現(xiàn)在已經(jīng)清楚了Annotation的整體作用,但是Annotation到底在開(kāi)發(fā)中能做哪些事情呢?為了進(jìn)一步理解Annotation的處理目的,下面將結(jié)合工廠設(shè)計(jì)模式來(lái)應(yīng)用Annotation操作。
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 ) //利用Annotation實(shí)現(xiàn)了類(lèi)的使用class MessageService{private IMessage message;public MessageService(){ UserMessage use=MessageService.class.getAnnotation(UserMessage.class);this.message = (IMessage)Factory.getInstance(use.clazz()); //直接通過(guò)Annotation獲取}public void send(String msg){this.msg.send(msg); } }class Factory {private Factory() {}public staticT getInstance(Class clazz){ //直接返回一個(gè)實(shí)例化對(duì)象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("【消息發(fā)送】"+msg); } }class NetMessageImpl implements IMessage {@Overridepublic void send(String msg) { System.out.println("【網(wǎng)絡(luò)消息發(fā)送】"+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("【代理操作】進(jìn)行消息發(fā)送通道的連接。");return true; }public void close() { System.out.println("【代理操作】關(guān)閉連接通道。"); }@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】消息無(wú)法進(jìn)行發(fā)送!"); } }finally {this.close(); } } }
執(zhí)行結(jié)果:
更換
@UserMessage(clazz =NetMessageImpl.class )
“如何整合工廠設(shè)計(jì)模式來(lái)應(yīng)用Annotation操作”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!