-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-classes-c.ts
113 lines (96 loc) · 2.22 KB
/
06-classes-c.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
108
109
110
111
112
113
(() => {
// Single Responsibility Principle
// Composition over inheritance
type Gender = 'M' | 'F';
interface PersonProps {
birthdate: Date;
gender: Gender;
name: string;
}
class Person {
public birthdate: Date;
public gender: Gender;
public name: string;
constructor({ name, gender, birthdate }: PersonProps) {
this.name = name;
this.gender = gender;
this.birthdate = birthdate;
}
}
interface UserProps {
email: string;
role: string;
}
class User {
public email: string;
public lastAccess: Date;
public role: string;
constructor({ email, role }: UserProps) {
this.email = email;
this.lastAccess = new Date();
this.role = role;
}
checkCredentials() {
return true;
}
}
interface UserSettingsProps {
lastOpenedFolder: string;
workingDirectory: string;
}
class Settings {
public lastOpenedFolder: string;
public workingDirectory: string;
constructor({
workingDirectory,
lastOpenedFolder,
}: UserSettingsProps) {
this.lastOpenedFolder = lastOpenedFolder;
this.workingDirectory = workingDirectory;
}
}
// Composition
interface SettingsProps {
birthdate: Date;
email: string;
gender: Gender;
lastOpenedFolder: string;
name: string;
role: string;
workingDirectory: string;
}
class UserSettings {
public person: Person;
public settings: Settings;
public user: User;
constructor({
name,
gender,
birthdate,
email,
role,
workingDirectory,
lastOpenedFolder,
}: SettingsProps) {
this.person = new Person({ name, gender, birthdate });
this.user = new User({ email, role });
this.settings = new Settings({ workingDirectory, lastOpenedFolder });
}
}
const newPerson = new Person({
name: 'John',
gender: 'M',
birthdate: new Date('1999-01-01'),
});
console.log(newPerson);
const userSettings = new UserSettings({
workingDirectory: '/home/john/',
lastOpenedFolder: '/home',
email: '[email protected]',
role: 'admin',
name: 'John',
gender: 'M',
birthdate: new Date('1999-01-01'),
});
console.log(userSettings)
})();