• Annotation整合工厂设计模式


    Annotation 是为了提供配置处理操作的,这些配置可以通过反射实现,本课程主要讲解 Annotation 与工厂设计模式的整合处理操作。

    代码如下:

    package com.anno.demo;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    interface IMessage{                //业务接口
        public void send(String msg);    //输出业务
    }
    
    class CloudMessageImpl 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 Factory{
        private Factory() {}
        public static <T> T getInstance(Class<T> clazz) {    //返回实例化对象
            try {    //利用反射获取实例化对象
                return (T) new MessageProxy().bind(clazz.getDeclaredConstructor().newInstance());
            } catch (Exception e) {
                return null;
            }
        }
    }
    
    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();
            }
        }
    }
    
    @Target({ElementType.TYPE, ElementType.METHOD})        //只能用在类和方法上
    @Retention(RetentionPolicy.RUNTIME)
    @interface UseMessage{
        public Class<?> clazz();    //定义要使用的类型
    }
    @UseMessage(clazz = CloudMessageImpl.class)    //Annotation定义使用类。红色部分可以修改为其他实现类,实现调用不同类输出。
    
    class MessageService{
        private IMessage message;        //定义业务处理
        public MessageService() {
            UseMessage use = MessageService.class.getAnnotation(UseMessage.class);
            this.message = (IMessage) Factory.getInstance(use.clazz());    //通过Annotation获取
        }
        public void send(String msg) {
            this.message.send(msg);
        }
    }
    
    public class Anno {
        public static void main(String[] args) {
            MessageService messageService = new MessageService();    //实例化接口对象
            messageService.send("www.sina.com.cn");    //调用方法
        }
    }

    运行结果:

    【代理操作】进行消息发送通道的连接.
    【云消息发送】www.sina.com.cn
    【代理操作】关闭连接通道.
  • 相关阅读:
    学习进度条
    阅读《实例化需求》10-12章有感
    学习进度条
    阅读《实例化需求》7–9章有感
    学习进度条
    软件需求与分析课堂讨论一
    课程引言课后作业1
    MVC实例应用模式
    MVC模式介绍
    二十三种设计模式
  • 原文地址:https://www.cnblogs.com/sunzhongyu008/p/11227187.html
Copyright © 2020-2023  润新知