Repository navigation
Expand file tree
/
Copy pathISmartDevice.ts
More file actions
85 lines (77 loc) · 2.27 KB
/
Copy pathISmartDevice.ts
File metadata and controls
85 lines (77 loc) · 2.27 KB
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
export interface ISmartDevice {
connectWifi(ssid: string): void;
openApp(path: string): void;
call(contact: string): void;
sendSms(contact: string, message: string): void;
}
export class SmartPhone implements ISmartDevice {
connectWifi(ssid: string): void {
console.log(`Connecting to wifi ${ssid}`);
}
openApp(path: string): void {
console.log(`Opening app ${path}`);
}
call(contact: string): void {
console.log(`Calling ${contact}`);
}
sendSms(contact: string, message: string): void {
console.log(`Sending ${message} to ${contact}`);
}
}
const phone = new SmartPhone();
phone.call("John");
phone.sendSms("John", "How are you");
phone.openApp("Instagram");
phone.connectWifi("home");
export class Tablet implements ISmartDevice {
connectWifi(ssid: string): void {
console.log(`Connecting to wifi ${ssid}`);
}
openApp(path: string): void {
console.log(`Opening app ${path}`);
}
/* Principle violated as we are forced to implement method that is not needed */
call(contact: string): void {
console.log(`Tablet cannot make a call`);
throw new Error("Method not implemented.");
}
/* Principle violated as we are forced to implement method that is not needed */
sendSms(contact: string, message: string): void {
console.log(`Tablet cannot send sms`);
throw new Error("Method not implemented.");
}
}
const tablet = new Tablet();
/*
To fix it, segregate interface into smaller interfaces
*/
export interface ISmartDevice2 {
connectWifi(ssid: string): void;
openApp(path: string): void;
}
export interface IPhoneDevice {
call(contact: string): void;
sendSms(contact: string, message: string): void;
}
export class SmartPhone2 implements ISmartDevice2, IPhoneDevice {
connectWifi(ssid: string): void {
console.log(`Connecting to wifi ${ssid}`);
}
openApp(path: string): void {
console.log(`Opening app ${path}`);
}
call(contact: string): void {
console.log(`Calling ${contact}`);
}
sendSms(contact: string, message: string): void {
console.log(`Sending ${message} to ${contact}`);
}
}
export class Tablet2 implements ISmartDevice2 {
connectWifi(ssid: string): void {
console.log(`Connecting to wifi ${ssid}`);
}
openApp(path: string): void {
console.log(`Opening app ${path}`);
}
}