com.qa.framework.InstanceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smart-api-framework Show documentation
Show all versions of smart-api-framework Show documentation
Support web service api automaton test based on testng and httpclient
package com.qa.framework;
import com.qa.framework.classfinder.ClassScanner;
import com.qa.framework.classfinder.impl.DefaultClassScanner;
import com.qa.framework.library.base.ReflectHelper;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 实例工厂
*/
public class InstanceFactory {
/**
* 用于缓存对应的实例
*/
private static final Map cache = new ConcurrentHashMap();
/**
* ClassScanner
*/
private static final String CLASS_SCANNER = "class_scanner";
/**
* 获取 ClassScanner
*
* @return the class scanner
*/
public static ClassScanner getClassScanner() {
return getInstance(CLASS_SCANNER, DefaultClassScanner.class);
}
/**
* Gets instance.
*
* @param the type parameter
* @param cacheKey the cache key
* @param defaultImplClass the default impl class
* @return the instance
*/
@SuppressWarnings("unchecked")
public static T getInstance(String cacheKey, Class defaultImplClass) {
// 若缓存中存在对应的实例,则返回该实例
if (cache.containsKey(cacheKey)) {
return (T) cache.get(cacheKey);
}
// 通过反射创建该实现类对应的实例
T instance = ReflectHelper.newInstance(defaultImplClass.getName());
// 若该实例不为空,则将其放入缓存
if (instance != null) {
cache.put(cacheKey, instance);
}
// 返回该实例
return instance;
}
}