The Observer is a design pattern where an object (known as a subject) maintains a list of objects depending on it (observers), automatically notifying them of any changes to state.
The Observer pattern requires that the observer (or object) wishing to receive topic notifications must subscribe this interest to the object firing the event (the subject).
One or more observers are interested in the state of a subject and register their interest with the subject by attaching themselves. When something changes in our subject that the observer may be interested in, a notify message is sent which calls the update method in each observer. When the observer is no longer interested in the subject's state, they can simply detach themselves
We will try to implement Observer pattern.
Observable.js
var EventEmitter = require('events').EventEmitter;
class Observable extends EventEmitter {
constructor() {
super()
}
RaiseEvent(event_name, event_args) {
console.log("Raising Event:" + event_name);
this.emit(event_name, event_args);
}
Subscribe(event_name, listener) {
console.log("Adding Listener for Event :" + event_name);
this.on(event_name, listener);
}
Unsubscribe(event_name, listener) {
console.log("Removing Listener for Event:" + event_name);
this.removeListener(event_name, listener);
}
ListenerCount(event_name)
{
this.listenerCount(this, event_name)
}
}
module.exports = Observable;
Testing the 'Observable' class.
index.js
var Observable = require('./Observable');
var observable = new Observable();
var greet1 = function (name) {
console.log('Hello ,' + name);
}
var greet2 = function (name) {
console.log('How are you,' + name);
}
var bye1 = function (name) {
console.log('Bye Bye ,' + name);
}
//two listeners get attached to 'greet' event
observable.Subscribe('greet', greet1);
observable.Subscribe('greet', greet2);
observable.Subscribe('bye', bye1);
console.log("Total listeners for 'greet' event = " + observable.listenerCount('greet') );
console.log("Total listeners for 'bye' event = " + observable.listenerCount('bye') );
//raising event for greet
observable.RaiseEvent('greet', 'sangram')
//raising event for swara
observable.RaiseEvent('greet', 'swara')
//raising event for sangram
observable.RaiseEvent('bye', 'sangram')
//remove greet1 listener
observable.Unsubscribe('greet', greet1)
console.log("Total listeners for 'greet' event = " + observable.listenerCount('greet'));
//raising event greet for sangram
observable.RaiseEvent('greet', 'sangram')
Run node index.js
output:
Adding Listener for Event :greet
Adding Listener for Event :greet
Adding Listener for Event :bye
Total listeners for 'greet' event = 2
Total listeners for 'bye' event = 1
Raising Event:greet
Hello ,sangram
How are you,sangram
Raising Event:greet
Hello ,swara
How are you,swara
Raising Event:bye
Bye Bye ,sangram
Removing Listener for Event:greet
Total listeners for 'greet' event = 1
Raising Event:greet
How are you,sangram
The Observer pattern requires that the observer (or object) wishing to receive topic notifications must subscribe this interest to the object firing the event (the subject).
One or more observers are interested in the state of a subject and register their interest with the subject by attaching themselves. When something changes in our subject that the observer may be interested in, a notify message is sent which calls the update method in each observer. When the observer is no longer interested in the subject's state, they can simply detach themselves
We will try to implement Observer pattern.
Observable.js
var EventEmitter = require('events').EventEmitter;
class Observable extends EventEmitter {
constructor() {
super()
}
RaiseEvent(event_name, event_args) {
console.log("Raising Event:" + event_name);
this.emit(event_name, event_args);
}
Subscribe(event_name, listener) {
console.log("Adding Listener for Event :" + event_name);
this.on(event_name, listener);
}
Unsubscribe(event_name, listener) {
console.log("Removing Listener for Event:" + event_name);
this.removeListener(event_name, listener);
}
ListenerCount(event_name)
{
this.listenerCount(this, event_name)
}
}
module.exports = Observable;
Testing the 'Observable' class.
index.js
var Observable = require('./Observable');
var observable = new Observable();
var greet1 = function (name) {
console.log('Hello ,' + name);
}
var greet2 = function (name) {
console.log('How are you,' + name);
}
var bye1 = function (name) {
console.log('Bye Bye ,' + name);
}
//two listeners get attached to 'greet' event
observable.Subscribe('greet', greet1);
observable.Subscribe('greet', greet2);
observable.Subscribe('bye', bye1);
console.log("Total listeners for 'greet' event = " + observable.listenerCount('greet') );
console.log("Total listeners for 'bye' event = " + observable.listenerCount('bye') );
//raising event for greet
observable.RaiseEvent('greet', 'sangram')
//raising event for swara
observable.RaiseEvent('greet', 'swara')
//raising event for sangram
observable.RaiseEvent('bye', 'sangram')
//remove greet1 listener
observable.Unsubscribe('greet', greet1)
console.log("Total listeners for 'greet' event = " + observable.listenerCount('greet'));
//raising event greet for sangram
observable.RaiseEvent('greet', 'sangram')
Run node index.js
output:
Adding Listener for Event :greet
Adding Listener for Event :greet
Adding Listener for Event :bye
Total listeners for 'greet' event = 2
Total listeners for 'bye' event = 1
Raising Event:greet
Hello ,sangram
How are you,sangram
Raising Event:greet
Hello ,swara
How are you,swara
Raising Event:bye
Bye Bye ,sangram
Removing Listener for Event:greet
Total listeners for 'greet' event = 1
Raising Event:greet
How are you,sangram
No comments:
Post a Comment