Skip to content

Commit c80a1ea

Browse files
committed
#29 - TypeScript
1 parent 920c965 commit c80a1ea

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Wrong way
2+
class Device {
3+
turnOn(): void {}
4+
turnOff(): void {}
5+
charge(): void {}
6+
connectToInternet(): void {}
7+
}
8+
9+
class Laptop extends Device {
10+
turnOn(): void {
11+
console.log("Laptop turned on");
12+
}
13+
14+
turnOff(): void {
15+
console.log("Laptop turned off");
16+
}
17+
18+
charge(): void {
19+
console.log("Laptop charging");
20+
}
21+
22+
connectToInternet(): void {
23+
console.log("Laptop connected to the internet");
24+
}
25+
}
26+
27+
class Microwave extends Device {
28+
turnOn(): void {
29+
console.log("Microwave turned on");
30+
}
31+
32+
turnOff(): void {
33+
console.log("Microwave turned off");
34+
}
35+
36+
charge(): void {
37+
throw new Error("A microwave doesn't need to be charged");
38+
}
39+
40+
connectToInternet(): void {
41+
throw new Error("A microwave doesn't need to be connected to the internet");
42+
}
43+
}
44+
45+
const microwave = new Microwave();
46+
// microwave.charge(); // Error
47+
48+
// Correct way
49+
class Switchable {
50+
turnOn(): void {}
51+
turnOff(): void {}
52+
}
53+
54+
class Chargeable {
55+
charge(): void {}
56+
}
57+
58+
class InternetConnectable {
59+
connectToInternet(): void {}
60+
}
61+
62+
class Laptop2 extends Switchable {
63+
turnOn(): void {
64+
console.log("Laptop turned on");
65+
}
66+
67+
turnOff(): void {
68+
console.log("Laptop turned off");
69+
}
70+
}
71+
72+
class AdvancedLaptop extends Laptop2 implements Chargeable, InternetConnectable {
73+
charge(): void {
74+
console.log("Laptop charging.");
75+
}
76+
77+
connectToInternet(): void {
78+
console.log("Laptop connected to the internet.");
79+
}
80+
}
81+
82+
class Microwave2 extends Switchable {
83+
turnOn(): void {
84+
console.log("Microwave turned on");
85+
}
86+
87+
turnOff(): void {
88+
console.log("Microwave turned off");
89+
}
90+
}
91+
92+
const laptop2 = new AdvancedLaptop();
93+
laptop2.charge();
94+
laptop2.connectToInternet();
95+
96+
const microwave2 = new Microwave2();
97+
microwave2.turnOn();
98+
microwave2.turnOff();
99+
100+
// Extra Exercise //
101+
class BlackAndWhitePrintable {
102+
printBlackAndWhite(): void {}
103+
}
104+
105+
class ColorPrintable {
106+
printColor(): void {}
107+
}
108+
109+
class Scannable {
110+
scan(): void {}
111+
}
112+
113+
class Faxable {
114+
sendFax(): void {}
115+
}
116+
117+
class BlackAndWhitePrinter extends BlackAndWhitePrintable {
118+
printBlackAndWhite(): void {
119+
console.log("Printing in black and white...");
120+
}
121+
}
122+
123+
class ColorPrinter extends ColorPrintable {
124+
printColor(): void {
125+
console.log("Printing in color...");
126+
}
127+
}
128+
129+
class MultiFunctionPrinter extends BlackAndWhitePrintable implements ColorPrintable, Scannable, Faxable {
130+
printBlackAndWhite(): void {
131+
console.log("Printing in black and white...");
132+
}
133+
134+
printColor(): void {
135+
console.log("Printing in color...");
136+
}
137+
138+
scan(): void {
139+
console.log("Scanning...");
140+
}
141+
142+
sendFax(): void {
143+
console.log("Sending fax...");
144+
}
145+
}
146+
147+
const blackAndWhitePrinter = new BlackAndWhitePrinter();
148+
blackAndWhitePrinter.printBlackAndWhite();
149+
150+
const colorPrinter = new ColorPrinter();
151+
colorPrinter.printColor();
152+
153+
const multiFunctionPrinter = new MultiFunctionPrinter();
154+
multiFunctionPrinter.printBlackAndWhite();
155+
multiFunctionPrinter.printColor();
156+
multiFunctionPrinter.scan();
157+
multiFunctionPrinter.sendFax();

0 commit comments

Comments
 (0)