|
12 | 12 | */
|
13 | 13 |
|
14 | 14 | abstract class EntertainmentDevice {
|
15 |
| - protected deviceState: number; |
16 |
| - protected maxSetting: number; |
17 |
| - protected volumeLevel: number = 0; |
| 15 | + protected volume: number = 0; |
18 | 16 |
|
19 |
| - public abstract buttonFivePressed(): void; |
20 |
| - public abstract buttonSixPressed(): void; |
| 17 | + constructor(protected setting: number, protected maxSetting: number) {} |
21 | 18 |
|
22 |
| - public deviceFeedback(): void { |
23 |
| - if (this.deviceState > this.maxSetting || this.deviceState < 0) { |
24 |
| - this.deviceState = 0; |
25 |
| - console.log(`On: ${this.deviceFeedback}`); |
| 19 | + abstract pressButtonFive(): void; |
| 20 | + abstract pressButtonSix(): void; |
| 21 | + |
| 22 | + feedback() { |
| 23 | + if (this.setting > this.maxSetting || this.setting < 0) { |
| 24 | + this.setting = 0; |
| 25 | + console.log(`On: ${this.setting}`); |
26 | 26 | }
|
27 | 27 | }
|
28 | 28 |
|
29 |
| - public buttonSevenPressed(): void { |
30 |
| - this.volumeLevel++; |
31 |
| - console.log(`Volume at: ${this.volumeLevel}`); |
| 29 | + pressButtonSeven() { |
| 30 | + this.volume++; |
| 31 | + console.log(`Volume at: ${this.volume}`); |
32 | 32 | }
|
33 | 33 |
|
34 |
| - public buttonEightPressed(): void { |
35 |
| - this.volumeLevel--; |
36 |
| - console.log(`Volume at: ${this.volumeLevel}`); |
| 34 | + pressButtonEight() { |
| 35 | + this.volume--; |
| 36 | + console.log(`Volume at: ${this.volume}`); |
37 | 37 | }
|
38 | 38 | }
|
39 | 39 |
|
40 | 40 | class TVDevice extends EntertainmentDevice {
|
41 |
| - public constructor(newDeviceState: number, newMaxSetting: number) { |
42 |
| - super(); |
43 |
| - this.deviceState = newDeviceState; |
44 |
| - this.maxSetting = newMaxSetting; |
45 |
| - } |
46 |
| - public buttonFivePressed(): void { |
| 41 | + pressButtonFive() { |
47 | 42 | console.log("Channel Down");
|
48 |
| - this.deviceState--; |
| 43 | + this.setting--; |
49 | 44 | }
|
50 |
| - public buttonSixPressed(): void { |
| 45 | + |
| 46 | + pressButtonSix() { |
51 | 47 | console.log("Channel Up");
|
52 |
| - this.deviceState++; |
| 48 | + this.setting++; |
53 | 49 | }
|
54 | 50 | }
|
55 | 51 |
|
56 | 52 | abstract class RemoteButton {
|
57 |
| - private theDevice: EntertainmentDevice; |
| 53 | + constructor(private device: EntertainmentDevice) {} |
58 | 54 |
|
59 |
| - public constructor(newDevice: EntertainmentDevice) { |
60 |
| - this.theDevice = newDevice; |
| 55 | + pressButtonFive() { |
| 56 | + this.device.pressButtonFive(); |
61 | 57 | }
|
62 | 58 |
|
63 |
| - public buttonFivePressed(): void { |
64 |
| - this.theDevice.buttonFivePressed(); |
| 59 | + pressButtonSix() { |
| 60 | + this.device.pressButtonSix(); |
65 | 61 | }
|
66 | 62 |
|
67 |
| - public buttonSixPressed(): void { |
68 |
| - this.theDevice.buttonSixPressed(); |
69 |
| - } |
70 |
| - |
71 |
| - public abstract buttonNinePressed(); |
| 63 | + abstract pressButtonNine(): void; |
72 | 64 | }
|
73 | 65 |
|
74 |
| -class TVRemoveMute extends RemoteButton { |
75 |
| - public constructor(newDevice: EntertainmentDevice) { |
76 |
| - super(newDevice); |
77 |
| - } |
78 |
| - |
79 |
| - public buttonNinePressed() { |
80 |
| - console.log("TV Muted"); |
| 66 | +class TVRemoteMute extends RemoteButton { |
| 67 | + pressButtonNine() { |
| 68 | + console.log("TV muted"); |
81 | 69 | }
|
82 | 70 | }
|
83 | 71 |
|
84 |
| -class TVRemovePause extends RemoteButton { |
85 |
| - public constructor(newDevice: EntertainmentDevice) { |
86 |
| - super(newDevice); |
87 |
| - } |
88 |
| - |
89 |
| - public buttonNinePressed() { |
| 72 | +class TVRemotePause extends RemoteButton { |
| 73 | + pressButtonNine() { |
90 | 74 | console.log("TV paused");
|
91 | 75 | }
|
92 | 76 | }
|
93 | 77 |
|
94 | 78 | //----------------------------------------------------------------------
|
95 |
| -const theTV: RemoteButton = new TVRemoveMute(new TVDevice(1, 200)); |
96 |
| -const theTV2: RemoteButton = new TVRemovePause(new TVDevice(1, 200)); |
97 |
| -theTV.buttonFivePressed(); |
98 |
| -theTV.buttonSixPressed(); |
99 |
| -theTV.buttonNinePressed(); |
100 | 79 |
|
101 |
| -theTV2.buttonNinePressed(); |
| 80 | +const device = new TVDevice(1, 200) |
| 81 | +const TV: RemoteButton = new TVRemoteMute(device); |
| 82 | +const TV2: RemoteButton = new TVRemotePause(device); |
| 83 | + |
| 84 | +TV.pressButtonFive(); |
| 85 | +TV.pressButtonSix(); |
| 86 | +TV.pressButtonNine(); |
| 87 | + |
| 88 | +TV2.pressButtonNine(); |
0 commit comments