com.kaka.notice.FacadeFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kaka-core Show documentation
Show all versions of kaka-core Show documentation
An event driven framework
The newest version!
package com.kaka.notice;
import com.kaka.util.ReflectUtils;
import java.util.HashMap;
import java.util.Map;
/**
* {@link Facade} 工厂
*
* @author zkpursuit
*/
public class FacadeFactory {
private static Class extends Facade> facadeClass = Facade.class;
private static final String DEFAULT = "default";
static final Map instanceMap = new HashMap<>();
/**
* 必须先调用此方法进行配置
* 一般在应用程序启动时调用,且仅调用一次
*
* @param facadeClass 事件总线类
* @param 事件总线实现类泛型限定
* @return 事件总线类实例
*/
public synchronized static T config(Class extends Facade> facadeClass) {
FacadeFactory.facadeClass = facadeClass;
return getFacade();
}
public synchronized static T getFacade(String name) {
Facade inst;
if (instanceMap.get(name) == null) {
try {
inst = ReflectUtils.newInstance(facadeClass);
inst.__name = name;
instanceMap.put(name, inst);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
inst = instanceMap.get(name);
}
return (T) inst;
}
public synchronized static T getFacade() {
return getFacade(DEFAULT);
}
public synchronized static boolean hasFacade(String name) {
return instanceMap.containsKey(name);
}
public synchronized static void removeFacade(String name) {
Facade inst = instanceMap.remove(name);
if (inst != null) {
inst.dispose();
}
}
}