Skip to content

Commit 926e9e5

Browse files
committed
[ts] refactor bridge
1 parent 604425a commit 926e9e5

File tree

1 file changed

+40
-53
lines changed

1 file changed

+40
-53
lines changed

Diff for: bridge-pattern/bridge-pattern.ts

+40-53
Original file line numberDiff line numberDiff line change
@@ -12,90 +12,77 @@
1212
*/
1313

1414
abstract class EntertainmentDevice {
15-
protected deviceState: number;
16-
protected maxSetting: number;
17-
protected volumeLevel: number = 0;
15+
protected volume: number = 0;
1816

19-
public abstract buttonFivePressed(): void;
20-
public abstract buttonSixPressed(): void;
17+
constructor(protected setting: number, protected maxSetting: number) {}
2118

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}`);
2626
}
2727
}
2828

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}`);
3232
}
3333

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}`);
3737
}
3838
}
3939

4040
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() {
4742
console.log("Channel Down");
48-
this.deviceState--;
43+
this.setting--;
4944
}
50-
public buttonSixPressed(): void {
45+
46+
pressButtonSix() {
5147
console.log("Channel Up");
52-
this.deviceState++;
48+
this.setting++;
5349
}
5450
}
5551

5652
abstract class RemoteButton {
57-
private theDevice: EntertainmentDevice;
53+
constructor(private device: EntertainmentDevice) {}
5854

59-
public constructor(newDevice: EntertainmentDevice) {
60-
this.theDevice = newDevice;
55+
pressButtonFive() {
56+
this.device.pressButtonFive();
6157
}
6258

63-
public buttonFivePressed(): void {
64-
this.theDevice.buttonFivePressed();
59+
pressButtonSix() {
60+
this.device.pressButtonSix();
6561
}
6662

67-
public buttonSixPressed(): void {
68-
this.theDevice.buttonSixPressed();
69-
}
70-
71-
public abstract buttonNinePressed();
63+
abstract pressButtonNine(): void;
7264
}
7365

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");
8169
}
8270
}
8371

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() {
9074
console.log("TV paused");
9175
}
9276
}
9377

9478
//----------------------------------------------------------------------
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();
10079

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

Comments
 (0)