From 1e45c3180add5d6d780c9b89b5a841b61751f55f Mon Sep 17 00:00:00 2001 From: fermin Date: Sun, 11 Feb 2024 21:25:38 +0100 Subject: [PATCH] Adapter pattern bridge --- patterns/bridge/device_options.go | 11 ++++++ patterns/bridge/radio.go | 43 +++++++++++++++++++++++ patterns/bridge/remote_control.go | 44 ++++++++++++++++++++++++ patterns/bridge/remote_control_test.go | 14 ++++++++ patterns/bridge/tv.go | 47 ++++++++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 patterns/bridge/device_options.go create mode 100644 patterns/bridge/radio.go create mode 100644 patterns/bridge/remote_control.go create mode 100644 patterns/bridge/remote_control_test.go create mode 100644 patterns/bridge/tv.go diff --git a/patterns/bridge/device_options.go b/patterns/bridge/device_options.go new file mode 100644 index 0000000..87dd0d5 --- /dev/null +++ b/patterns/bridge/device_options.go @@ -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 +} diff --git a/patterns/bridge/radio.go b/patterns/bridge/radio.go new file mode 100644 index 0000000..25c35c2 --- /dev/null +++ b/patterns/bridge/radio.go @@ -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{} +} diff --git a/patterns/bridge/remote_control.go b/patterns/bridge/remote_control.go new file mode 100644 index 0000000..65d361e --- /dev/null +++ b/patterns/bridge/remote_control.go @@ -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) +} diff --git a/patterns/bridge/remote_control_test.go b/patterns/bridge/remote_control_test.go new file mode 100644 index 0000000..5c1b22b --- /dev/null +++ b/patterns/bridge/remote_control_test.go @@ -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()) +} diff --git a/patterns/bridge/tv.go b/patterns/bridge/tv.go new file mode 100644 index 0000000..fdbe508 --- /dev/null +++ b/patterns/bridge/tv.go @@ -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 +}