-
Notifications
You must be signed in to change notification settings - Fork 74
/
Chain of Responsibility.js
49 lines (42 loc) · 1.18 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
'use strict';
class Handler {
constructor() {
console.log('Handler Class created');
}
handleRequest() {
console.log('Handler.handleRequest invoked');
}
}
class ConcreteHandler1 extends Handler {
constructor() {
super();
console.log('ConcreteHandler1 Class created');
}
setSuccessor(successor) {
this.successor = successor;
console.log('ConcreteHandler1.setSuccessor invoked');
}
handleRequest(request) {
console.log('ConcreteHandler1.handleRequest invoked');
if (request === 'run')
console.log('ConcreteHandler1 has handled the request');
else {
console.log('ConcreteHandler1 calls his successor');
this.successor.handleRequest(request);
}
}
}
class ConcreteHandler2 extends Handler {
constructor() {
super();
console.log('ConcreteHandler2 Class created');
}
handleRequest(request) {
console.log('ConcreteHandler2.handleRequest invoked');
}
}
let handle1 = new ConcreteHandler1();
let handle2 = new ConcreteHandler2();
handle1.setSuccessor(handle2);
handle1.handleRequest('run');
handle1.handleRequest('stay');