zoukankan      html  css  js  c++  java
  • 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
    【代理操作】关闭连接通道.
  • 相关阅读:
    树型表的设计 上海
    FTP通讯封装 上海
    线程淡写 上海
    TCP通讯故障 上海
    设计模式引导 上海
    初试Delegate 上海
    c# 扫描端口 上海
    攻读计算机研究生的看法(转载) 上海
    挖掘表字段中的汉字 上海
    新生活运动 上海
  • 原文地址:https://www.cnblogs.com/sunzhongyu008/p/11227187.html
Copyright © 2011-2022 走看看