-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.ts
48 lines (42 loc) · 1022 Bytes
/
chain.ts
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
interface IHandler {
handle(any: string): void,
setNext(handler: IHandler): IHandler;
}
abstract class AbstractHandler implements IHandler {
private nextHandler!: IHandler;
setNext(nextHandler: IHandler) {
this.nextHandler = nextHandler;
return this.nextHandler;
}
handle(any: string) {
if (this.nextHandler) {
this.nextHandler.handle(any);
} else {
console.log('end', any);
}
}
}
class CatHandler extends AbstractHandler {
handle(any: string) {
if (any === 'cat') {
console.log('yes, i cat');
} else {
super.handle(any);
}
}
}
class DogHandler extends AbstractHandler {
handle(any: string) {
if (any === 'dog') {
console.log('yes, i dog');
} else {
super.handle(any);
}
}
}
const cat = new CatHandler();
const dog = new DogHandler();
cat.setNext(dog);
cat.handle('dog');
cat.handle('cat');
cat.handle('fdsfs');