1 Star 0 Fork 0

yclxiao / specialty

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
EventBus.java 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
yclxiao 提交于 2020-04-13 11:21 . 简易EventBus
package com.ycl.blog.designmode.eventbus;
import java.util.List;
import java.util.concurrent.Executor;
/**
* User: 杨成龙
* Date: 2020/4/13
* Time: 9:16 上午
* Desc: 消息总线入口方法
*/
public class EventBus {
private ObserverRegistry registry = new ObserverRegistry();
private Executor executor;
public EventBus() {
}
public EventBus(Executor executor) {
this.executor = executor;
}
/**
* 注册观察者
*/
public void register(Object observer) {
registry.register(observer);
}
/**
* 发布者-发送消息
*/
public void post(Object event) {
List<ObserverAction> observerActions = registry.getMatchedObserverActions(event);
for (ObserverAction observerAction : observerActions) {
if (executor == null) {
observerAction.execute(event);
} else {
executor.execute(() -> {
observerAction.execute(event);
});
}
}
}
}
Java
1
https://gitee.com/yclxiao/specialty.git
git@gitee.com:yclxiao/specialty.git
yclxiao
specialty
specialty
master

搜索帮助