最近公司做的项目是POS点菜机,根据需求发现用EventBus比较麻烦,
(一个消息只能一个订阅者接收,我想做的是,一个地方发消息,多处可以同时接收 )
所以就自己写了一个EventBusPlus
public class EventBusPlus {
private static final Map<String,Map<String ,SubscriberMethod>> CLASS_REGISTER_EVENT = new ConcurrentHashMap<>();
private static final Map<String,Map<String ,SubscriberMethod>> TAG_REGISTER_EVENT = new ConcurrentHashMap<>();
public static final String TAG = EventBusPlus.class.getSimpleName();
private static EventBusPlus eventBusPlus;
private EventBusPlus(){}
public synchronized static EventBusPlus getEventBusPlus(){
if(eventBusPlus == null){
synchronized (EventBusPlus.class){
if(eventBusPlus == null){
eventBusPlus = new EventBusPlus();
}
}
}
return eventBusPlus;
}
/**
* 给指定的类发送消息
* @param targetClass 目标类的class
* @param tag
*/
public void postEventMessageByClass(Class<?> targetClass, String tag, final Object... param){
if(targetClass == null || tag == null){
return;
}
String className = targetClass.getName();
if(CLASS_REGISTER_EVENT.containsKey(className)){
Map<String, SubscriberMethod> subscriberMethodMap = CLASS_REGISTER_EVENT.get(className);
if(subscriberMethodMap.containsKey(tag)){
final SubscriberMethod subscriberMethod = subscriberMethodMap.get(tag);
invokeMet