3 Observer Pattern implementations
Blogs20172017-04-21
3 Observer Pattern implementations
I extract The Observer pattern from https://en.wikipedia.org/wiki/Observer\_pattern:
There is a helpful article for this:
Comparison between different Observer Pattern implementations
1. Event Emitter/Target/Dispatcher
myObject.addEventListener('myCustomEventTypeString', handler);
myObject.dispatchEvent(new Event('myCustomEventTypeString'));
myObject.removeEventListener('myCustomEventTypeString', handler);2.Publish / Subscribe (pub/sub)
globalBroadcaster.subscribe('myCustomEventTypeString', handler);
globalBroadcaster.publish('myCustomEventTypeString', param1, param2, ...);
globalBroadcaster.unsubscribe('myCustomEventTypeString', handler);3. Signals
myObject.myCustomEventType.add(handler);
myObject.myCustomEventType.dispatch(param1, param2, ...);
myObject.myCustomEventType.remove(handler);