-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample1.ts
41 lines (33 loc) · 883 Bytes
/
sample1.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
/**
* Implementation of Prototype design pattern in typescript.
*
* Prototype is a creational design pattern that lets you copy existing objects
* without making your code dependent on their classes.
*/
abstract class Human {
name: string;
address: string;
job: string;
protected constructor(_human: Human) {
this.name = _human?.name;
this.job = _human?.job;
this.address = _human?.address;
}
abstract clone(): Human
}
class Doctor extends Human {
direction: string;
constructor(_doctor?: Doctor) {
super(_doctor);
this.direction = 'Healthcare';
}
clone(): Doctor {
return new Doctor(this);
}
}
const doctor = new Doctor();
doctor.name = 'Ali';
doctor.job = 'Healthcare specialist';
doctor.address = 'Samarkand';
const newDoctor = doctor.clone();
newDoctor.name = 'Vali';