美文网首页
2023-07-21

2023-07-21

作者: 皓皓amous | 来源:发表于2023-07-20 11:01 被阅读0次

    public class IoManager {
    private volatile static IoManager inStance;
    private IoTestInface ioTestInface;

    private IoManager() {
    }
    
    private static Map<String, Class> classMap = new HashMap<>();
    
    static {
        classMap.put("key", RequestTest.class);
    }
    
    public static IoManager getInstance() {
        if (inStance == null) {
            synchronized (IoManager.class) {
                if (inStance == null) {
                    inStance = new IoManager();
                }
            }
        }
        return inStance;
    }
    
    public IoTestInface getRequestTest(String key) {
        if (TextUtils.isEmpty(key)) {
            return null;
        }
        Set<Map.Entry<String, Class>> entries = classMap.entrySet();
        Iterator<Map.Entry<String, Class>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Class> next = iterator.next();
            String keyStr = next.getKey();
            Class value = next.getValue();
            if (keyStr.equals(key)) {
                try {
                    ioTestInface = (IoTestInface) value.newInstance();
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InstantiationException e) {
                    throw new RuntimeException(e);
                }
                break;
            }
    
        }
        return ioTestInface;
    };
    
    public void setRequest(String key) {
        IoTestInface requestTest = getRequestTest(key);
        requestTest.reqeuest("dsds");
    }
    

    public interface IoTestInface {
    void reqeuest(String reqe);
    }

    public class RequestTest implements IoTestInface {
    @Override
    public void reqeuest(String reqe) {

    }
    

    }

    相关文章

      网友评论

          本文标题:2023-07-21

          本文链接:https://www.haomeiwen.com/subject/latzudtx.html