Different implementations of the observer
The following implementations are available for the observer pattern:
Event dispatcher/listener
Publish/subscribe
Let's understand both the implementations in detail.
Event dispatcher/listener
The event dispatcher/listener (event emitter) implementation allows broadcasting data to more than one consumer. EventEmitter
can be inherited in the case of more than one event represented by one object. We can use the on
function for the EventEmitter
implementation in the observer pattern to define objects for listening to an event. Objects in the event dispatcher implementation uses custom events that can be inherited from an event dispatcher object. It uses different kind of strings to identify the type of the event.
The following is the sample code for the event dispatcher/listener:
var EventEmitter = require("events").EventEmitter; var eventEmitter = new EventEmitter(); eventEmitter.on("touch",function() { Console.log("Touch event has occured"); }); eventEmitter...