Last active
April 25, 2016 22:41
-
-
Save pbse/746a872f13beb781155d2c7af699a188 to your computer and use it in GitHub Desktop.
Observer Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Main Observabale class | |
| var Observable = function() { | |
| this.subscribers = []; | |
| } | |
| Observable.prototype = { | |
| subscribe: function(Subscriber) { | |
| if (this.subscribers.indexOf(Subscriber) == -1) { | |
| this.subscribers.push(Subscriber); | |
| } else throw "Subscriber Already Present."; | |
| log("Subscriber " + Subscriber.getName() + " Registered."); | |
| return true; | |
| }, | |
| unsubscribe: function(UnSubscriber) { | |
| if (this.subscribers.indexOf(UnSubscriber) == -1) { | |
| throw "Subscriber " + UnSubscriber.getName() + " Not Found."; | |
| } else { | |
| for (var i = 0; i < this.subscribers.length; i++) { | |
| if (this.subscribers[i] === UnSubscriber) { | |
| this.subscribers.splice(i, 1); | |
| log("Subscriber " + UnSubscriber.getName() + " UnRegistered."); | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| }, | |
| publish: function(message) { | |
| if (message) { | |
| this.subscribers.forEach(function(item, index, array) { | |
| item.read(message); | |
| }) | |
| return true; | |
| } | |
| return false; | |
| }, | |
| showSubscribers: function() { | |
| this.subscribers.forEach(function(item, index, array) { | |
| log("Subscriber - " + (index + 1) + " : Name is - " + item.getName()); | |
| }); | |
| } | |
| }; | |
| // Subscriber Class. One Could add more options to it and use Inheritance as well | |
| var Subscriber = function(name) { | |
| this.name = name; | |
| }; | |
| Subscriber.prototype = { | |
| getName: function() { | |
| return this.name; | |
| }, | |
| setName: function(name) { | |
| this.name = name; | |
| }, | |
| read: function(message) { | |
| if (message) { | |
| log(this.name + " recieved the message - " + message); | |
| return true; | |
| } | |
| return false; | |
| } | |
| }; | |
| // Test Cases | |
| // Register an Observable | |
| var observer = new Observable(); | |
| // Create Some Subscribers | |
| var subs1 = new Subscriber("Tony"); | |
| var subs2 = new Subscriber("Anthony"); | |
| var subs3 = new Subscriber("Martial"); | |
| // Subscribe The Created Subscribers | |
| observer.subscribe(subs1); | |
| observer.subscribe(subs2); | |
| observer.subscribe(subs3); | |
| // Publish a message to all | |
| observer.publish("Stock Price gets Cheap"); | |
| // Show Subscribers | |
| observer.showSubscribers(); | |
| // Unsubscribe | |
| observer.unsubscribe(subs2); | |
| // Another Message Published | |
| observer.publish("Stock Price gets Higher"); | |
| function log(message) { | |
| console.log(message); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment