Skip to content

Commit

Permalink
Adapter pattern bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
ferminhg committed Feb 11, 2024
1 parent 4f10e45 commit 1e45c31
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
11 changes: 11 additions & 0 deletions patterns/bridge/device_options.go
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
}
43 changes: 43 additions & 0 deletions patterns/bridge/radio.go
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{}
}
44 changes: 44 additions & 0 deletions patterns/bridge/remote_control.go
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)
}
14 changes: 14 additions & 0 deletions patterns/bridge/remote_control_test.go
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())
}
47 changes: 47 additions & 0 deletions patterns/bridge/tv.go
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
}

0 comments on commit 1e45c31

Please sign in to comment.