1. 程式人生 > >事件聚合器【其他模式】

事件聚合器【其他模式】

事件聚合器模式

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);
        }
    }
}