迭代器设计模式写的一个小工具

根据迭代器模式,查出实体类全类名,NEW出这个对象并返回
package tanji.test.Service;

import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import tanji.test.Bean.Car;
import tanji.test.Bean.Obj;
import tanji.test.Bean.Phone;

import javax.servlet.annotation.WebInitParam;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;


public  class fullPAth implements Iterable {
    //索引
    private int index = 0;
    private List p_list;
    private Object obj = null;
    public  fullPAth(String key) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        List<String> list = getPathList(key);
        p_list = list;
    }

    public fullPAth() {
    }

    //实现Iterable接口中要求实现的方法
    @Override
    public Iterator iterator() {
        return new MyIterator();//返回一个MyIterator实例对象
    }

    public Object newKey(){
        while (iterator().hasNext()) {
            obj = iterator().next();
        }
        return obj;
    }

    //内部类继承迭代器 重写方法
    class  MyIterator implements Iterator {
        @Override
        public boolean hasNext() {
            //只要在调用next()后,index自加,确保index的长度
            return index != p_list.size();
        }

        @SneakyThrows
        @Override
        public Object next() {
             Object o = null;
            try {
                //获取容器里的参数
                Class<?> clazz = newObj((String) p_list.get(index));
                //反射对象
                 o = clazz.newInstance();
                //执行方法
                Method logBanner = clazz.getMethod("logBanner");
                logBanner.invoke(o);
                System.out.println("==========Run    OK===========");
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("==========Run error===========");
            }
            //移动指针
            index++;
            return o;
        }

        @Override
        public void forEachRemaining(Consumer action) {

        }
    }

    /**
     * 1 通过传入KEY 获取b_path 得到list集合
     * @param key
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public  static List<String> getPathList(String key) throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/ceshi?useUnicode=true&characterEncoding=utf-8";
        Connection connection = DriverManager.getConnection(url, "root", "123456");
        PreparedStatement ps = connection.prepareStatement("SELECT * FROM b_bean WHERE b_name = ?");
        ps.setString(1, key);
        ResultSet rs = ps.executeQuery();
        List<String> list = new ArrayList();
        while (rs.next()) {
            String b_path = rs.getString("b_path");
            list.add(b_path);
        }
        return list;
    }

    /**
     * 根据传入path 反射实体类对象并返回
     * @param className
     * @param args
     * @return
     */
    public static Class<?> newObj(String className, Object... args) {
        Class[] classes = new Class[args.length];
        for (int i = 0; i < classes.length; i++) {
            classes[i] = args[i].getClass();
        }
        Class<?> aClass = null;
        try {
            aClass = Class.forName(className);
            Object o = aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aClass ;
    }


}

 

迭代器设计模式写的一个小工具

上一篇:视图与表


下一篇:H3C交换机批量端口配置