-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconceptual.js
More file actions
83 lines (81 loc) · 3.08 KB
/
conceptual.js
File metadata and controls
83 lines (81 loc) · 3.08 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Some commands may just implements simple operations on their own.
var SimpleCommand = /** @class */ (function () {
function SimpleCommand(payload) {
this.payload = payload;
}
SimpleCommand.prototype.execute = function () {
console.log("SimpleCommand: See, I can do simple things like printing (".concat(this.payload, ")"));
};
return SimpleCommand;
}());
// While some commands delegate their work to the Reciever
var ComplexCommand = /** @class */ (function () {
// Complex commands accept the reciever object and context data
function ComplexCommand(receiver, a, b) {
this.receiver = receiver;
this.a = a;
this.b = b;
}
// Commands are able to delegate to any method of the receiver
ComplexCommand.prototype.execute = function () {
console.log('ComplexCommand: Complex stuff should be done by a receiver object.');
this.receiver.doSomething(this.a);
this.receiver.doSomethingElse(this.b);
};
return ComplexCommand;
}());
// The Receiver classes contain some important business logic.
var Receiver = /** @class */ (function () {
function Receiver() {
}
Receiver.prototype.doSomething = function (a) {
console.log("Receiver: Working on (".concat(a, ".)"));
};
Receiver.prototype.doSomethingElse = function (b) {
console.log("Receiver: Also working on (".concat(b, ".)"));
};
return Receiver;
}());
// The Invoker is associated with commands and delgates work to them
var Invoker = /** @class */ (function () {
function Invoker() {
}
// Initialize commands for start and finish
Invoker.prototype.setOnStart = function (command) {
this.onStart = command;
};
Invoker.prototype.setOnFinish = function (command) {
this.onFinish = command;
};
// Invoker passes requests indirectly through commands
Invoker.prototype.doSomethingImportant = function () {
console.log('Invoker: Does anybody want something done before I begin?');
if (this.isCommand(this.onStart)) {
this.onStart.execute();
}
console.log('\nInvoker: ...doing something really important...\n');
console.log('Invoker: Does anybody want something done after I finish?');
if (this.isCommand(this.onFinish)) {
this.onFinish.execute();
}
};
Invoker.prototype.isCommand = function (object) {
return object.execute !== undefined;
};
return Invoker;
}());
// Client code
var invoker = new Invoker();
invoker.setOnStart(new SimpleCommand('Printed Message'));
var receiver = new Receiver();
invoker.setOnFinish(new ComplexCommand(receiver, 'Task one', 'Task two'));
invoker.doSomethingImportant();
/* OUTPUT
Invoker: Does anybody want something done before I begin?
SimpleCommand: See, I can do simple things like printing (Printed Message)
Invoker: ...doing something really important...
Invoker: Does anybody want something done after I finish?
ComplexCommand: Complex stuff should be done by a receiver object.
Receiver: Working on (Task one.)
Receiver: Also working on (Task two.)
*/