设计模式-桥接模式

桥接模式核心原理是将抽象部分和实现部分分析,使二者可以独立的变化。再具体一点,当某个类型由于自身逻辑可以有两个或多个维度的变化,使用桥接模式可以令这些维度变化时不影响其他维度。再举个例子,手机——可以按照品牌来分类,比如苹果华为三星,也可以按照功能分类,比如拍照手机,游戏手机等等。

桥接模式有2个设计原则:

is-a

实例代码如下:

package main

import "fmt"

type phoneFeature interface {
    showFeature()
}

type gamePhone struct{}

func (g gamePhone) showFeature() {
    fmt.Printf("This phone is for play game!\n")
}

type cameraPhone struct{}

func (c cameraPhone) showFeature() {
    fmt.Printf("This phone is for camera!\n")
}

type phoneBrand struct {
    name    string
    feature phoneFeature
}

func (b phoneBrand) show() {
    fmt.Printf("This is a %s phone!\n", b.name)
    b.feature.showFeature()
}

func (b *phoneBrand) setFeature(phonefeature phoneFeature) {
    b.feature = phonefeature
}

type huawei struct {
    *phoneBrand
}

type apple struct {
    *phoneBrand
}

func main() {
    h := huawei{phoneBrand: &phoneBrand{name: "huawei"}}
    a := apple{phoneBrand: &phoneBrand{name: "apple"}}

    h.setFeature(cameraPhone{})
    h.show()

    a.setFeature(gamePhone{})
    a.show()
}

这里我创建了2种类型的手机:游戏手机和拍照手机,2种品牌:华为和苹果。然后通过 phoneFeature 接口作为桥梁将这2者组合在一起,通过这种方法,不论以后是增加修改手机品牌还是类型,都不会影响到另一方了。

使用场景

  1. 类的抽象和实现都应该可以通过生成子类的方法加以扩充
  2. 对一个抽象的实现部分的修改应对客户不产生影响。