-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
91 lines (77 loc) · 1.7 KB
/
index.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
interface Device {
isEnabled(): boolean
enable(): void
disable(): void
getVolume(): number
setVolume(percent: number): void
getChannel(): number
setChannel(channel: number): void
}
class RemoteControle {
private device: Device
constructor(device: Device) {
this.device = device
}
togglePower() {
if (this.device.isEnabled()) {
this.device.disable()
console.log('Disable device...');
} else {
this.device.enable()
console.log('Enable device...');
}
}
volumeDown() {
this.device.setVolume(this.device.getVolume() - 10)
console.log('Volume down...');
}
volumeUp() {
this.device.setVolume(this.device.getVolume() + 10)
console.log('Volume Up...');
}
channelDown() {
this.device.setChannel(this.device.getChannel() - 1)
console.log('Channel down...');
}
channelUp() {
this.device.setChannel(this.device.getChannel() + 1)
console.log('Channel up...');
}
}
class Tv implements Device {
private on: boolean
private volume: number
private channel: number
isEnabled(): boolean {
return this.on ? true : false
}
enable(): void {
this.on = true
}
disable(): void {
}
getVolume(): number {
return this.volume
}
setVolume(percent: number): void {
this.volume = percent
}
getChannel(): number {
return this.channel
}
setChannel(channel: number): void {
this.channel = channel
}
}
function clientCodeContext() {
const tv = new Tv()
const simpleRemote = new RemoteControle(tv)
simpleRemote.togglePower()
simpleRemote.volumeUp()
simpleRemote.volumeUp()
simpleRemote.channelDown()
simpleRemote.togglePower()
}
export function bridge() {
clientCodeContext()
}