각 구독자가 여러 가지 유형의 이벤트를 기다리는 게시 - 구독 시스템에서 간단한 스위치보다 나은 처리 솔루션이 있습니까?게시 - 가입 시스템에서 이벤트를 처리하는 다른 방법은 무엇입니까?
두 게시자 인 Pub1 및 Pub2가 있다고 가정 해 보겠습니다. Pub1은 두 가지 유형의 이벤트 Pub1-eventA 및 Pub1-eventB를 전송합니다. Pub2-eventA 및 Pub2-eventB와 Pub2도 동일합니다.
한편 Pub1 및 Pub2의 이벤트를 구독하는 클라이언트 Sub1이 있습니다.
Sub1 수신기에서 이러한 이벤트를 어떻게 처리합니까? 여기
몇 가지 가능성 :
이한 수신기, 하나의 큰 스위치 (하드가 유지) :
Listener{
HandleEvent(event){
if(event.type == Pub1-eventA)
Action1.execute();
if(event.type == Pub1-eventB)
Action2.execute();
if(event.type == Pub2-eventA)
Action3.execute();
if(event.type == Pub2-eventB)
Action4.execute();
}
}
한 청취자와의 연관지도 : 이벤트 유형에 따라
이Map<event-type, Action> ActionMap
Listener{
Action = ActionMap[event-type]
Action.execute();
}
한 리스너 :
ListenerPub1-eventA{ check(event-type); Action1.execute(); }
ListenerPub1-eventB{ check(event-type); Action2.execute(); }
ListenerPub2-eventA{ check(event-type); Action3.execute(); }
ListenerPub2-eventB{ check(event-type); Action4.execute(); }
이것이 최고의 솔루션이라고 생각합니다. 그리고 청취자의 goups에 대해 어떻게 생각하십니까? – codablank