Created
February 24, 2016 22:22
-
-
Save reccanti/aacb0185407089e6a646 to your computer and use it in GitHub Desktop.
JavaScript Observer Pattern
This file contains 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
/** | |
* This module describes a Calculator. | |
* The calculator performs a single calculation of a | |
* given function, then sends the output to a list of | |
* observers. | |
*/ | |
var Calculator = (function() { | |
/** | |
* The function that will be used for calculation | |
*/ | |
var calculateFunction; | |
/** | |
* A list of observers | |
*/ | |
var observers = []; | |
/** | |
* Sends an object to all of the observers | |
*/ | |
function broadcastToObservers(obj) { | |
for (var i = 0; i < observers.length; i++) { | |
observers[i].observeFunc(obj); | |
} | |
} | |
/** | |
* Returns a function that can be instantiated | |
*/ | |
return function() { | |
this.addObserver = function (o) { | |
observers.push(o); | |
} | |
/** | |
* Sets the function that will be used for calculation | |
*/ | |
this.setCalculateFunction = function(f) { | |
calculateFunction = f; | |
}; | |
/** | |
* Performs a calculation for a given number of | |
* iterations. | |
*/ | |
this.calculate = function(iterations) { | |
var state = {}; | |
var data = {}; | |
for (var i = 0; i < iterations; i++) { | |
calculateFunction(state); | |
// quick and dirty clone of state | |
data = JSON.parse(JSON.stringify(state)); | |
broadcastToObservers(data); | |
} | |
}; | |
} | |
})(); | |
/** | |
* This is a Sum module. It is meant to observe the | |
* Calculator. Whenever the calculator performs a calculation, | |
* it performs the designated observer function. | |
*/ | |
var Sum = (function() { | |
return function() { | |
var sum = 0; | |
/** | |
* This is the function that will be called in Calculator. | |
* It takes the data and adds it to the sum | |
*/ | |
this.observeFunc = function(data) { | |
sum += data.x; | |
} | |
/** | |
* Returns the current calculated sum | |
*/ | |
this.getSum = function() { | |
return sum; | |
} | |
} | |
})(); | |
/** | |
* This is a Prduct module. It is meant to observe the | |
* Calculator. Whenever the calculator performs a calculation, | |
* it performs the designated observer function. | |
*/ | |
var Product = (function() { | |
return function() { | |
var product = 1; | |
/** | |
* This is the function that will be called in Calculator. | |
* It takes the data and adds it to the sum | |
*/ | |
this.observeFunc = function(data) { | |
product *= data.x; | |
} | |
/** | |
* Returns the current calculated sum | |
*/ | |
this.getProduct = function() { | |
return product; | |
} | |
} | |
})(); | |
/** | |
* Create new instances of the Sum object, Product object, | |
* and the calculator object. | |
*/ | |
var sumInstance = new Sum(); | |
var productInstance = new Product(); | |
var calculatorInstance = new Calculator(); | |
/** | |
* Add the sum object as an observer to the calculator and create the | |
* calculate function. This function adds one the current state. | |
*/ | |
calculatorInstance.addObserver(sumInstance); | |
calculatorInstance.addObserver(productInstance); | |
calculatorInstance.setCalculateFunction(function(state) { | |
if (!state.x) { | |
state.x = 1; | |
} | |
state.x++; | |
}); | |
/** | |
* Rund the calculations 5 times and output the sm to the console | |
*/ | |
calculatorInstance.calculate(5); | |
console.log(sumInstance.getSum()); // 20 | |
console.log(productInstance.getProduct()); // 720 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment