-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain-of-responsibility.js
76 lines (68 loc) · 1.67 KB
/
chain-of-responsibility.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
69
70
71
72
73
74
75
76
let Dispatcher = {
handleRequest: function (request) {
handlerList = [new Strategy1(), new Strategy2(), new Strategy3(), new Strategy4()];
for (var i = 0; i < handlerList.length - 1; i++) {
handlerList[i].setNext(handlerList[i + 1])
}
handlerList[0].handleRequest(request);
}
};
//Handler
var Handler = function () {
this.next = {
handleRequest: function (request) {
console.log('All strategies exhausted.');
}
}
};
Handler.prototype.setNext = function (next) {
this.next = next;
return next;
};
Handler.prototype.handleRequest = function (request) {
};
//Strategy1
var Strategy1 = function () {
};
Strategy1.prototype = new Handler();
Strategy1.prototype.handleRequest = function (request) {
console.log('Strategy1');
if (request == 1) {
return;
}
this.next.handleRequest(request);
};
//Strategy2
var Strategy2 = function () {
};
Strategy2.prototype = new Handler();
Strategy2.prototype.handleRequest = function (request) {
console.log('Strategy2');
if (request == 2) {
alert('I am handled');
return;
}
this.next.handleRequest(request);
};
//Strategy3
var Strategy3 = function () {
};
Strategy3.prototype = new Handler();
Strategy3.prototype.handleRequest = function (request) {
console.log('Strategy3');
if (request == 3) {
return;
}
this.next.handleRequest(request);
}
//Strategy4
var Strategy4 = function () {
};
Strategy4.prototype = new Handler();
Strategy4.prototype.handleRequest = function (request) {
console.log('Strategy4');
if (request == 4) {
return;
}
this.next.handleRequest(request);
};