桥接模式

桥接模式

音频接口

package bridge

type Video interface {
    decode(string)
}

AVI格式

package bridge

type AVI struct {}

func NewAVI() *AVI {
    return &AVI{}
}

func (a *AVI)decode(name string) {
    fmt.Printf("AVI 格式进行解码%s\n",name)
}

MP3格式

package bridge

type MP3 struct {}

func NewMP3() *MP3 {
    return &MP3{}
}

func(m *MP3)decode(name string) {
    fmt.Printf("MP3 格式进行解码%s\n",name)
}

操作系统接口

package bridge

type System interface{
    play(string)
}

Windows

package bridge

type Mac struct {
    video Video
}

func NewMac(video Video) System{
    return &Mac{video:video}
}

func (m *Mac)play(name string) {
    m.video.decode(name)
    fmt.Println("Mac 开始播放音乐")
}

Linux

package bridge

type Linux struct {
    video Video
}

func NewLinux(video Video) System{
    return &Linux{video:video}
}

func (l *Linux)play(name string) {
    l.video.decode(name)
    fmt.Println("Linux 开始播放音乐")
}

测试文件

package bridge

func TestBridge(t *testing.T){
    sys := NewMac(NewMP3())
    sys.play("小苹果")
    sys = NewLinux(NewAVI())
    sys.play("偶吧刚囊死他")
}
posted @ 2022-09-22 10:03  理科土拨鼠  阅读(20)  评论(0)    收藏  举报