zoukankan      html  css  js  c++  java
  • 代码片段,反射组装责任链

    @Test
    public void testChain() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, IOException {
        
        List<Class> implClassList=getImplClass(GetCode.class);
        Class getClass=implClassList.get(0);
        Class[] classes = new Class[]{GetCode.class};
        Object[] objects=new Object[]{null};
        Constructor constructor = getClass.getConstructor(classes);
        GetCode getCode = (GetCode)constructor.newInstance(objects);
        for (int i = 1; i < implClassList.size(); i++) {
           
                objects = new Object[]{getCode};
            
            constructor = implClassList.get(i).getConstructor(classes);
             getCode=(GetCode)constructor.newInstance(objects);
            
        }
        String o=getCode.code("用户自定义操作", "");
        if (o.getClass().toString().matches(".+String$")) {
            log.info(o.toString());
        } else {
            log.info(o.getClass());
            
        }
    }

    抽象了一个方法,如果用户自己添加类实现了,处理了,就走他的路子

    没人实现,最后到我的默认处理类。

    给一个配置文件,让用户写上自己的实现类包名

    然后扫描到报名下实现接口的类,一一加到责任链里面来。责任链最后一个就是自己的默认处理类。

  • 相关阅读:
    mysql高级之编程优化
    高性能产品必由之路
    linux下安装xhprof
    linux下安装apc
    linux下安装vld
    python装饰器通俗易懂的解释!
    python函数基础 与文件操作
    python基础入门一(语法基础)
    iOS Keychain,SSKeychain,使用 理解 原理
    起头
  • 原文地址:https://www.cnblogs.com/zhangminjie/p/4349175.html
Copyright © 2011-2022 走看看