-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.js
More file actions
26 lines (21 loc) · 781 Bytes
/
strategy.js
File metadata and controls
26 lines (21 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
Strategy Pattern
This is the receiver for the command pattern which is abstracted out further to use the
strategy pattern
*/
function StrategyInterface() {}
StrategyInterface.prototype.sort = function(numbers) {
// The subclass will shadow this function thus emulating an interface behaviour
throw "Must implement algorithm function";
}
StrategyInterface.prototype.numberWithCommas = function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function StrategyContext(strategy) {
this.strategy = strategy;
}
StrategyContext.prototype.executeStrategy = function(numbers) {
this.strategy.sort(numbers);
};
exports.StrategyInterface = StrategyInterface;
exports.StrategyContext = StrategyContext;