質問

Regarding the event driven programming model. Normally the employer raise the question "simulate the event driven programming model in Java without any external library". I think it is a simple question and can answered with usage of design pattern.

My solutions is have 2 queue in place as the publish subscribe(aka. observer) channel. publish channel will register event targeted to particular event type such as click.

poll element -> button 1 click || button 1 click again || button 1 click certain time. <- publisher queue

another subscribe channel will store the event listener, if new publish comes up,

poll element -> listener 1 || listener 2 || -< subscriber queue.

it will push into the publish queue first and poll it on another time allow subscriber to consume. The subscribe will consume the publisher element and apply to each element in subscriber channel (in this case each event listener). Observer design pattern allows component agnostic. publisher doesn't know anything of subscriber.

This scenario resembles email newsletter subscription.

In the OO implementation, there will be Publishers object and Subscribers class. Actually a queue of detailed publishers implementation and detailed subscriber implementation. Also the Mediator design pattern is incorporated to coordinate publishers class and subscribers class.

Is there any flaw on my solution?

役に立ちましたか?

解決

No, not really. Your solution is pretty standard and sound.

Observer/subscriber for basic events and Mediator if you need to coordinate sending events in a specific order.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top