天天看點

整合工廠設計模式與Annotation | 帶你學《Java語言進階特性》之九十八

上一篇:反射取得Annotation資訊 | 帶你學《Java語言進階特性》之九十七

【本節目标】

本節介紹了工廠設計模式與Annotation整合的案例應用。

整合工廠設計模式與Annotation

現在已經清楚了Annotation的整體作用,但是Annotation到底在開發中能做哪些事情呢?為了進一步了解Annotation的處理目的,下面将結合工廠設計模式來應用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實作了類的使用
class MessageService{
    private IMessage message;
    public MessageService(){
        UserMessage use=MessageService.class.getAnnotation(UserMessage.class);
        this.message = (IMessage)Factory.getInstance(use.clazz());  //直接通過Annotation擷取
    }
    public void send(String msg){
        this.msg.send(msg);
    }
}
class Factory  {
    private Factory() {}
    public static <T> T getInstance(Class<T> clazz){   //直接傳回一個執行個體化對象
        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 {
    @Override
    public void send(String msg) {
        System.out.println("【消息發送】"+msg);
    }
}
class NetMessageImpl implements IMessage {
    @Override
    public void send(String msg) {
        System.out.println("【網絡消息發送】"+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("【代理操作】進行消息發送通道的連接配接。");
        return true;
    }
    public void close() {
        System.out.println("【代理操作】關閉連接配接通道。");
    }
    @Override
    public 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】消息無法進行發送!");
            }      
        }finally {
            this.close();
        }
    }
}           

執行結果:

整合工廠設計模式與Annotation | 帶你學《Java語言進階特性》之九十八

更換

@UserMessage(clazz =NetMessageImpl.class )           
整合工廠設計模式與Annotation | 帶你學《Java語言進階特性》之九十八

由于Annotation的存在,是以面向接口的程式設計處理将可以直接利用Annotation的屬性完成控制,進而使得整體代碼變得整潔。

想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。

本内容視訊來源于

阿裡雲大學 下一篇:類集架構簡介 | 帶你學《Java語言進階特性》之九十九 更多Java面向對象程式設計文章檢視此處