-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.1 Chain of Responsibility.ts
107 lines (88 loc) · 2.87 KB
/
3.1 Chain of Responsibility.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
interface Handler<R, H> {
handle: (request: R) => void;
setNext: (handler: H) => void;
}
interface Patient {
symptomsOf: 'superficial skin cut' | 'acne' | 'melanoma' | string;
}
abstract class Doctor implements Handler<Patient, Doctor> {
protected next?: Doctor;
constructor() {}
handle(patient: Patient): void {
if (this.next) this.next.handle(patient);
else this.terminateChain();
}
terminateChain(): void {
console.log('The patient was redirected to a clinic, specialized on their condition.');
}
setNext(handler: Doctor): void {
this.next = handler;
}
}
class GeneralPractitioner extends Doctor {
constructor() {
super();
}
handle(patient: Patient): void {
if (patient.symptomsOf === 'superficial skin cut') {
console.log('The condition of the patient was treated by a general practitioner.');
} else if (this.next) {
this.next.handle(patient);
} else {
this.terminateChain();
}
}
}
class Dermatologist extends Doctor {
constructor() {
super();
}
handle(patient: Patient): void {
if (patient.symptomsOf === 'acne') {
console.log('The condition of the patient was treated by a dermatologist.');
} else if (this.next) {
this.next.handle(patient);
} else {
this.terminateChain();
}
}
}
class DermatologistOncologist extends Doctor {
constructor() {
super();
}
handle(patient: Patient): void {
if (patient.symptomsOf === 'melanoma') {
console.log('The condition of the patient was treated by a dermatologist, specialized in Oncology.');
} else if (this.next) {
this.next.handle(patient);
} else {
this.terminateChain();
}
}
}
class DermatologyClinic {
private specialists: Doctor[];
private firstLevelSpecialist: Doctor;
constructor() {
const gp = new GeneralPractitioner();
const dermatologist = new Dermatologist();
const oncodermatologist = new DermatologistOncologist();
gp.setNext(dermatologist);
dermatologist.setNext(oncodermatologist);
this.firstLevelSpecialist = gp;
this.specialists = [gp, dermatologist, oncodermatologist];
}
admit(patient: Patient): void {
this.firstLevelSpecialist.handle(patient);
}
}
const clinic = new DermatologyClinic();
const patient1: Patient = { symptomsOf: 'superficial skin cut' };
const patient2: Patient = { symptomsOf: 'acne' };
const patient3: Patient = { symptomsOf: 'melanoma' };
const patient4: Patient = { symptomsOf: 'chronic kidney disease' };
clinic.admit(patient1); // 'The condition of the patient was treated by a general practitioner.'
clinic.admit(patient2); // 'The condition of the patient was treated by a dermatologist.'
clinic.admit(patient3); // 'The condition of the patient was treated by a dermatologist, specialized in Oncology.'
clinic.admit(patient4); // 'The patient was redirected to a clinic, specialized on their condition.'