事件聚合器模式
public class EventAggregator {
/**
* 事件聚合器模式:
* A system with lots of objects can lead to complexities
* when a client wants to subscribe to events.
* The client has to find and register for each object individually,
* if each object has multiple events then each event requires a separate subscription.
* An Event Aggregator acts as a single source of events for many objects.
* It registers for all the events of the many objects allowing clients to register with just the aggregator.
* 当客户端想要订阅事件时,一个有大量对象的系统会导致复杂性。
* 客户端需要单独查找每个对象并进行注册。
* 如果每个对象都有多个事件,那么每个事件都需要单独的订阅。
* 事件聚合器充当许多对象的单个事件源,
* 事件聚合器将注册到所有的事件生成器中,客户端只需要注册到事件聚合器中即可获得通知。
*/
@Test
public void all() {
final KingJoffrey kingJoffrey = new KingJoffrey();
final KingsHand kingsHand = new KingsHand(kingJoffrey);
final List<BaseEventEmitter> emitters = Lists.newArrayList();
// 将事件聚合器注册到事件生成器中
emitters.add(new LordBaelish(kingsHand));
emitters.add(new LordVarys(kingsHand));
emitters.add(new Scout(kingsHand));
Stream.of(Weekday.values()).forEach(day -> emitters.forEach(emitter -> emitter.timePasses(day)));
}
}
/**
* 1)具体的事件类型
*/
@AllArgsConstructor
@Getter
enum Event {
STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED("Traitor detected");
private String description;
}
/**
* 2)事件监听器
*/
interface EventListener {
void onEvent(Event event);
}
/**
* 3)事件发射器接口
*/
interface EventEmitter {
void registerObserver(EventListener eventListener);
void notifyObservers(Event event);
}
@AllArgsConstructor
@Getter
enum Weekday {
MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY(
"Saturday"), SUNDAY("Sunday");
private String description;
}
/**
* 4)事件发射器抽象
*/
abstract class BaseEventEmitter implements EventEmitter {
// 持有多个事件监听器
private final List<EventListener> eventListeners;
public BaseEventEmitter() {
eventListeners = Lists.newArrayList();
}
@Override
public void registerObserver(EventListener eventListener) {
eventListeners.add(eventListener);
}
@Override
public void notifyObservers(Event event) {
eventListeners.forEach(listener -> listener.onEvent(event));
}
public abstract void timePasses(Weekday day);
}
/**
* 5)单纯的事件监听器
*/
@Slf4j
class KingJoffrey implements EventListener {
@Override
public void onEvent(Event e) {
log.info("Received event from the King's Hand: {}", e.toString());
}
}
/**
* 6)组合事件监听器和事件发射器
*/
class KingsHand extends BaseEventEmitter implements EventListener {
public KingsHand(EventListener eventListener) {
registerObserver(eventListener);
}
@Override
public void onEvent(Event event) {
notifyObservers(event);
}
@Override
public void timePasses(Weekday day) {
// NOP
}
}
class LordBaelish extends BaseEventEmitter {
public LordBaelish(EventListener eventListener) {
registerObserver(eventListener);
}
@Override
public void timePasses(Weekday day) {
if (day.equals(Weekday.FRIDAY)) {
notifyObservers(Event.STARK_SIGHTED);
}
}
}
class LordVarys extends BaseEventEmitter {
public LordVarys(EventListener eventListener) {
registerObserver(eventListener);
}
@Override
public void timePasses(Weekday day) {
if (day.equals(Weekday.SATURDAY)) {
notifyObservers(Event.TRAITOR_DETECTED);
}
}
}
class Scout extends BaseEventEmitter {
public Scout(EventListener eventListener) {
registerObserver(eventListener);
}
@Override
public void timePasses(Weekday day) {
if (day.equals(Weekday.TUESDAY)) {
notifyObservers(Event.WARSHIPS_APPROACHING);
}
}
}