-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
72 lines (58 loc) · 2.41 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var Numbers = require('./numbers').Numbers
, StrategyContext = require('./strategy').StrategyContext
, MergeSort = require('./sorting-algorithms/mergesort').MergeSort
, InsertionSort = require('./sorting-algorithms/insertionsort').InsertionSort
, QuickSortRandomPivot = require('./sorting-algorithms/quicksort').QuickSortRandomPivot;
/*
Command Pattern: Command Interface
Used to enforce the implementation of execute. Also, used for tagging objects; The invoker
will only run commands that are an instance of this interface.
*/
function CommandInterface() {}
CommandInterface.prototype.execute = function() {
throw "Must implement the execute function";
};
/*
Command Pattern: Invoker
*/
var SortCommandInvoker = function() {
var arrayOfCommands = []
this.store = function(command) {
if(!(command instanceof CommandInterface)) {
throw "This is not a valid command";
}
arrayOfCommands.push(command);
}
this.execute = function() {
arrayOfCommands.forEach(function(command, index, array) {
command.execute();
})
}
}
exports.SortCommandInvoker = SortCommandInvoker;
/*
Command Pattern: Concrete Commands
Used to enforce the implementation of execute. Also, used for tagging objects; The invoker
will only run commands that are an instance of this interface.
*/
var MergeSortCommand = function() {};
MergeSortCommand.prototype = new CommandInterface();
MergeSortCommand.prototype.execute = function() {
var strategyContext = new StrategyContext(new MergeSort());
strategyContext.executeStrategy(new Numbers(10000000, "random"));
};
var InsertionSortCommand = function() {};
InsertionSortCommand.prototype = new CommandInterface();
InsertionSortCommand.prototype.execute = function() {
var strategyContext = new StrategyContext(new InsertionSort());
strategyContext.executeStrategy(new Numbers(10000, "random"));
};
var QuickSortRandomPivotCommand = function() {};
QuickSortRandomPivotCommand.prototype = new CommandInterface();
QuickSortRandomPivotCommand.prototype.execute = function() {
var strategyContext = new StrategyContext(new QuickSortRandomPivot());
strategyContext.executeStrategy(new Numbers(10000000, "random"));
};
exports.MergeSortCommand = MergeSortCommand;
exports.InsertionSortCommand = InsertionSortCommand;
exports.QuickSortRandomPivotCommand = QuickSortRandomPivotCommand;