|
| 1 | +// build the Subject base class |
| 2 | +var window = window || null; |
| 3 | +var Subject = ( function( window, undefined ) { |
| 4 | + |
| 5 | + function Subject() { |
| 6 | + this._list = []; |
| 7 | + } |
| 8 | + |
| 9 | + // this method will handle adding observers to the internal list |
| 10 | + Subject.prototype.observe = function observeObject( obj ) { |
| 11 | + console.log( 'added new observer' ); |
| 12 | + this._list.push( obj ); |
| 13 | + }; |
| 14 | + |
| 15 | + //this method will remove an existing observer form internal list |
| 16 | + Subject.prototype.unobserve = function unobserveObject( obj ) { |
| 17 | + for( var i = 0, len = this._list.length; i < len; i++ ) { |
| 18 | + if( this._list[ i ] === obj ) { |
| 19 | + this._list.splice( i, 1 ); |
| 20 | + console.log( 'removed existing observer' ); |
| 21 | + return true; |
| 22 | + } |
| 23 | + } |
| 24 | + return false; |
| 25 | + }; |
| 26 | + |
| 27 | + Subject.prototype.notify = function notifyObservers() { |
| 28 | + var args = Array.prototype.slice.call( arguments, 0 ); |
| 29 | + for( var i = 0, len = this._list.length; i < len; i++ ) { |
| 30 | + this._list[ i ].update(args ); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + return Subject; |
| 35 | + |
| 36 | +} )( window ); |
| 37 | + |
| 38 | +// setup an object that fetchs stocks |
| 39 | +function StockGrabber() { |
| 40 | + |
| 41 | + var subject = new Subject(); |
| 42 | + |
| 43 | + this.addObserver = function addObserver( newObserver ) { |
| 44 | + subject.observe( newObserver ); |
| 45 | + }; |
| 46 | + |
| 47 | + this.removeObserver = function removeObserver( deleteObserver ) { |
| 48 | + subject.unobserve( deleteObserver ); |
| 49 | + }; |
| 50 | + |
| 51 | + this.fetchStocks = function fetchStocks() { |
| 52 | + // fake fetching the stocks |
| 53 | + var stocks = { |
| 54 | + aapl : 167.00, |
| 55 | + goog : 243.67, |
| 56 | + msft : 99.34 |
| 57 | + }; |
| 58 | + |
| 59 | + // notify our observers of the stock change |
| 60 | + subject.notify( stocks ); |
| 61 | + }; |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +// define a couple of different observers |
| 66 | +var StockUpdaterComponent = { |
| 67 | + update : function() { |
| 68 | + console.log( '"update" called on StockUpdater with: ', arguments ); |
| 69 | + } |
| 70 | +}; |
| 71 | +var StockChartsComponent = { |
| 72 | + update : function() { |
| 73 | + console.log( '"update" called on StockCharts with: ', arguments ); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +// example usage |
| 78 | +var stockApp = new StockGrabber(); |
| 79 | + |
| 80 | +//add the StockUpdaterComponent to observer list |
| 81 | +stockApp.addObserver( StockUpdaterComponent ); |
| 82 | +stockApp.fetchStocks(); // console logs: "update" called on StockUpdaterComponent with... |
| 83 | + |
| 84 | +//add the StockChartsComponent to observer list |
| 85 | +stockApp.addObserver( StockChartsComponent ); |
| 86 | +stockApp.fetchStocks(); // console logs: "update" called on StockUpdater with... "update" called on StockCarts with... |
| 87 | + |
| 88 | +//remove the stockUpdaterComponent form observer list |
| 89 | +stockApp.removeObserver( StockUpdaterComponent ); |
| 90 | +stockApp.fetchStocks(); // console logs: "update" called on StockCharts with... |
| 91 | + |
| 92 | +//remove the stockChartsComponent form observer list |
| 93 | +stockApp.removeObserver( StockChartsComponent ); |
| 94 | +stockApp.fetchStocks(); // does nothing; no observers |
0 commit comments