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) {
}
}
网友评论