-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package bridge | ||
|
||
type Device interface { | ||
IsEnabled() bool | ||
Enable() bool | ||
Disable() bool | ||
GetVolume() int | ||
SetVolume(int) bool | ||
GetChannel() int | ||
SetChannel(int) bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bridge | ||
|
||
type Radio struct { | ||
enabled bool | ||
volume int | ||
channel int | ||
} | ||
|
||
func (r *Radio) IsEnabled() bool { | ||
return r.enabled == true | ||
} | ||
|
||
func (r *Radio) Enable() bool { | ||
r.enabled = true | ||
return true | ||
} | ||
|
||
func (r *Radio) Disable() bool { | ||
r.enabled = false | ||
return false | ||
} | ||
|
||
func (r *Radio) GetVolume() int { | ||
return r.volume | ||
} | ||
|
||
func (r *Radio) SetVolume(volume int) bool { | ||
r.volume = volume | ||
return true | ||
} | ||
|
||
func (r *Radio) GetChannel() int { | ||
return r.channel | ||
} | ||
|
||
func (r *Radio) SetChannel(i int) bool { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func NewRadio() *Radio { | ||
return &Radio{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package bridge | ||
|
||
type RemoteControl struct { | ||
device Device | ||
} | ||
|
||
func NewRemoteControl(device Device) *RemoteControl { | ||
return &RemoteControl{device: device} | ||
} | ||
|
||
func (r *RemoteControl) TogglePower() bool { | ||
if r.device.IsEnabled() { | ||
return r.device.Disable() | ||
} | ||
return r.device.Enable() | ||
} | ||
|
||
func (r *RemoteControl) VolumeDown() bool { | ||
return r.device.SetVolume(r.device.GetVolume() - 10) | ||
} | ||
|
||
func (r *RemoteControl) VolumeUp() bool { | ||
return r.device.SetVolume(r.device.GetVolume() + 10) | ||
} | ||
|
||
func (r *RemoteControl) ChannelDown() bool { | ||
return r.device.SetChannel(r.device.GetChannel() - 1) | ||
} | ||
|
||
func (r *RemoteControl) ChannelUp() bool { | ||
return r.device.SetChannel(r.device.GetChannel() + 1) | ||
} | ||
|
||
type AdvancedRemoteControl struct { | ||
RemoteControl | ||
} | ||
|
||
func NewAdvancedRemoteControl(device Device) *AdvancedRemoteControl { | ||
return &AdvancedRemoteControl{RemoteControl{device: device}} | ||
} | ||
|
||
func (a *AdvancedRemoteControl) Mute() bool { | ||
return a.device.SetVolume(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bridge | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRemoteControl(t *testing.T) { | ||
rcTV := NewRemoteControl(NewTV()) | ||
assert.True(t, rcTV.TogglePower()) | ||
|
||
rcRadio := NewAdvancedRemoteControl(NewRadio()) | ||
assert.True(t, rcRadio.TogglePower()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package bridge | ||
|
||
type TV struct { | ||
enabled bool | ||
volume int | ||
channel int | ||
} | ||
|
||
func NewTV() *TV { | ||
return &TV{ | ||
enabled: false, | ||
volume: 0, | ||
channel: 0, | ||
} | ||
} | ||
|
||
func (t *TV) IsEnabled() bool { | ||
return t.enabled == true | ||
} | ||
|
||
func (t *TV) Enable() bool { | ||
t.enabled = true | ||
return t.enabled | ||
} | ||
|
||
func (t *TV) Disable() bool { | ||
t.enabled = false | ||
return true | ||
} | ||
|
||
func (t *TV) GetVolume() int { | ||
return t.volume | ||
} | ||
|
||
func (t *TV) SetVolume(volume int) bool { | ||
t.volume = volume | ||
return true | ||
} | ||
|
||
func (t *TV) GetChannel() int { | ||
return t.channel | ||
} | ||
|
||
func (t *TV) SetChannel(i int) bool { | ||
t.channel = i | ||
return true | ||
} |