【设计模式】桥接,是设计模式?对,其实你用过

桥接模式是结构型设计模式

GoF定义:将抽象部分与它的实现部分分离,使它们都可以独立地变化。

优点:用组合/聚合的方式来共享一些能用的方法

这个模式非常像网卡的桥接模式,把两个不同的局域网通过网卡连接起来。

简单示例

先看一个简单的示例:

go 复制代码
package main  
  
import"fmt" 

type Implementor interface {  
    OperationImp()  
}  
  
type ConcreteImplementorA struct{}  
  
func NewConcreteImplementorA() *ConcreteImplementorA {  
    return &ConcreteImplementorA{}  
}  
  
func (c *ConcreteImplementorA) OperationImp() {  
    fmt.Println("具体实现A")  
}  
  
type ConcreteImplementorB struct{}  
  
func NewConcreteImplementorB() *ConcreteImplementorB {  
    return &ConcreteImplementorB{}  
}  
  
func (c *ConcreteImplementorB) OperationImp() {  
    fmt.Println("具体实现A")  
}  
  
type RefinedAbstraction struct {  
    Imp Implementor  
}  
  
func NewRefinedAbstraction() *RefinedAbstraction {  
    return &RefinedAbstraction{}  
}  
  
func (r *RefinedAbstraction) Operation() {  
    r.Imp.OperationImp()  
}  
func (r *RefinedAbstraction) SetImplementor(imp Implementor) {  
    r.Imp = imp  
}

func main() {  
    implementorA := NewConcreteImplementorA()  
    implementorB := NewConcreteImplementorB()  
  
    refinedAbstraction := NewRefinedAbstraction()  
  
    refinedAbstraction.SetImplementor(implementorA)  
    refinedAbstraction.Operation()  
  
    refinedAbstraction.SetImplementor(implementorB)  
    refinedAbstraction.Operation()  
}

上述这段代码,可以很容易理解。
SetImplementor(implementorA)SetImplementor(implementorB)

可以动态的切换 Operation() 的实现,也就是把 Operation() 进行了抽象 。

实际案例

场景

会员系统,需要在用户订阅到货通知后,引导用户关注公众号,具体要求如下:

1.PC端:订阅了商品到货通知后,判断用户是否关注了公众号,没关注,需要展示公众号的二维码;已关注,不用展示。

2.微信小程序客户端:订阅了商品到货通知后,判断用户是否关注了公众号,没关注,需打开微信公众号文章,文章中会展示出 公众号的二维码,用户可以长按进行关注;已关注,不用展示。

3.手机浏览器H5客户端:订阅了商品到货通知后,不需要判断是否关注公众号。

对于不同端进行不同的操作,可以把这部分进行抽象。通过调用方法进行切换。

定义接口

go 复制代码
type ISubscribe interface {  
    Subscribe(agent, productName string)  
}

按照不同的客户端进行 Subscribe 方法的定制

PC端

go 复制代码
type PCAlertStockSubscribe struct{}  
  
func NewPCAlertStockSubscribe() ISubscribe {  
    return &PCAlertStockSubscribe{}  
}  
  
func (p *PCAlertStockSubscribe) Subscribe(agent, productName string) {  
    fmt.Println("记录到货知订阅")  
    fmt.Printf("客户端: %v, 商品: %v, 到货通知订阅成功\n", agent, productName)  
  
    fmt.Println("判断是否需引导用户关注公众号")  
    fmt.Println("获取微信公众号的二维码")  
    fmt.Println("引导用户关注公众号,展示微信公众号二维码")  
}

微信小程序端

go 复制代码
  
type MiniProgramAlertStockSubscribe struct{}  
  
func NewMiniProgramAlertStockSubscribe() ISubscribe {  
    return &MiniProgramAlertStockSubscribe{}  
}  
  
func (m *MiniProgramAlertStockSubscribe) Subscribe(agent, productName string) {  
    fmt.Println("记录到货知订阅")  
    fmt.Printf("客户端: %v, 商品: %v, 到货通知订阅成功\n", agent, productName)  
  
    fmt.Println("判断是否需引导用户关注公众号")  
    fmt.Println("获取微信公众号的文章的连接")  
    fmt.Println("引导用户关注公众号,打开微信公众号文章,可以长按二维码进行关注")  
}

手机浏览器H5端

go 复制代码
type MobileAlertStockSubscribe struct{}  
  
func NewMobileAlertStockSubscribe() ISubscribe {  
    return &MobileAlertStockSubscribe{}  
}  
  
func (m *MobileAlertStockSubscribe) Subscribe(agent, productName string) {  
    fmt.Println("记录到货知订阅")  
    fmt.Printf("客户端: %v, 商品: %v, 到货通知订阅成功\n", agent, productName)  
}

通过 ProductAlertStockSubscribe 进行切换

go 复制代码
type IProductSubscribe interface {  
    SetImpSubscribe(imp ISubscribe)  
    ExecSubscribe(agent, productName string)  
}  
  
type ProductAlertStockSubscribe struct {  
    Imp ISubscribe  
}  
  
func NewProductAlertStockSubscribe() IProductSubscribe {  
    return &ProductAlertStockSubscribe{}  
}  
  
func (p *ProductAlertStockSubscribe) SetImpSubscribe(imp ISubscribe) {  
    p.Imp = imp  
}  
  
func (p *ProductAlertStockSubscribe) ExecSubscribe(agent, productName string) {  
    p.Imp.Subscribe(agent, productName)  
}

ProductAlertStockSubscribe 就可以类比网卡,调用 SetImpSubscribe 方法 ,让不同端的逻辑进行切换。

还需要定义一个映射关系,用于切换的客户端,这部分其实是策略模式

go 复制代码
type clientSubscribeFunc func() ISubscribe  
  
const (  
    WebAgent          = "web"  
    WapAgent          = "wap"  
    MiniProgramAgent  = "miniProgram"  
)  
  
var clientAlertStockSubscribeMap = map[string]clientSubscribeFunc{  
    WebAgent:         NewPCAlertStockSubscribe,  
    MiniProgramAgent: NewMiniProgramAlertStockSubscribe,  
    WapAgent:         NewMobileAlertStockSubscribe,  
}

按照不同的客户端进行调用

go 复制代码
func main() {  
    productName := "【设计模式:可复用面向对象软件的基础】"  
  
    productAlertStockSubscribe := NewProductAlertStockSubscribe()  
    agents := []string{WebAgent, MiniProgramAgent, WapAgent}  
    for _, agent := range agents {  
       // 策略模式的体现  
       alertStockSubscribeFunc := clientAlertStockSubscribeMap[agent]  
       alertStockSubscribe := alertStockSubscribeFunc()  
  
       // 桥接模式  
       productAlertStockSubscribe.SetImpSubscribe(alertStockSubscribe)  
       productAlertStockSubscribe.ExecSubscribe(agent, productName)  
       fmt.Println()  
    }  
}

完整代码如下:

go 复制代码
package main

import "fmt"

type ISubscribe interface {  
    Subscribe(agent, productName string)  
}  
  
type PCAlertStockSubscribe struct{}  
  
func NewPCAlertStockSubscribe() ISubscribe {  
    return &PCAlertStockSubscribe{}  
}  
  
func (p *PCAlertStockSubscribe) Subscribe(agent, productName string) {  
    fmt.Println("记录到货知订阅")  
    fmt.Printf("客户端: %v, 商品: %v, 到货通知订阅成功\n", agent, productName)  
  
    fmt.Println("判断是否需引导用户关注公众号")  
    fmt.Println("获取微信公众号的二维码")  
    fmt.Println("引导用户关注公众号,展示微信公众号二维码")  
}  
  
type MiniProgramAlertStockSubscribe struct{}  
  
func NewMiniProgramAlertStockSubscribe() ISubscribe {  
    return &MiniProgramAlertStockSubscribe{}  
}  
  
func (m *MiniProgramAlertStockSubscribe) Subscribe(agent, productName string) {  
    fmt.Println("记录到货知订阅")  
    fmt.Printf("客户端: %v, 商品: %v, 到货通知订阅成功\n", agent, productName)  
  
    fmt.Println("判断是否需引导用户关注公众号")  
    fmt.Println("获取微信公众号的文章的连接")  
    fmt.Println("引导用户关注公众号,打开微信公众号文章,可以长按二维码进行关注")  
}  
  
type MobileAlertStockSubscribe struct{}  
  
func NewMobileAlertStockSubscribe() ISubscribe {  
    return &MobileAlertStockSubscribe{}  
}  
  
func (m *MobileAlertStockSubscribe) Subscribe(agent, productName string) {  
    fmt.Println("记录到货知订阅")  
    fmt.Printf("客户端: %v, 商品: %v, 到货通知订阅成功\n", agent, productName)  
}  
  
type IProductSubscribe interface {  
    SetImpSubscribe(imp ISubscribe)  
    ExecSubscribe(agent, productName string)  
}  
  
type ProductAlertStockSubscribe struct {  
    Imp ISubscribe  
}  
  
func NewProductAlertStockSubscribe() IProductSubscribe {  
    return &ProductAlertStockSubscribe{}  
}  
  
func (p *ProductAlertStockSubscribe) SetImpSubscribe(imp ISubscribe) {  
    p.Imp = imp  
}  
  
func (p *ProductAlertStockSubscribe) ExecSubscribe(agent, productName string) {  
    p.Imp.Subscribe(agent, productName)  
}  
  
type clientSubscribeFunc func() ISubscribe  
  
const (  
    WebAgent         = "web"  
    WapAgent         = "wap"  
    MiniProgramAgent = "miniProgram"  
)  
  
var clientAlertStockSubscribeMap = map[string]clientSubscribeFunc{  
    WebAgent:         NewPCAlertStockSubscribe,  
    MiniProgramAgent: NewMiniProgramAlertStockSubscribe,  
    WapAgent:         NewMobileAlertStockSubscribe,  
}

func main() {  
    productName := "【设计模式:可复用面向对象软件的基础】"  
  
    productAlertStockSubscribe := NewProductAlertStockSubscribe()  
    agents := []string{WebAgent, MiniProgramAgent, WapAgent}  
    for _, agent := range agents {  
       // 策略模式的体现  
       alertStockSubscribeFunc := clientAlertStockSubscribeMap[agent]  
       alertStockSubscribe := alertStockSubscribeFunc()  
  
       // 桥接模式  
       productAlertStockSubscribe.SetImpSubscribe(alertStockSubscribe)  
       productAlertStockSubscribe.ExecSubscribe(agent, productName)  
       fmt.Println()  
    }  
}

总结

怎么样?是不是觉得很熟悉,是不是感觉已经使用过了,只是不知道这个是桥接模式。

体现了对扩展开放、修改关闭 设计原则

后续再次遇到这样类似的,我想你应该知道该怎么做了。

相关推荐
William一直在路上14 分钟前
SpringBoot 拦截器和过滤器的区别
hive·spring boot·后端
花好月圆春祺夏安40 分钟前
基于odoo17的设计模式详解---备忘模式
数据库·设计模式
小马爱打代码1 小时前
Spring Boot 3.4 :@Fallback 注解 - 让微服务容错更简单
spring boot·后端·微服务
曾曜2 小时前
PostgreSQL逻辑复制的原理和实践
后端
豌豆花下猫2 小时前
Python 潮流周刊#110:JIT 编译器两年回顾,AI 智能体工具大爆发(摘要)
后端·python·ai
轻语呢喃2 小时前
JavaScript :事件循环机制的深度解析
javascript·后端
ezl1fe2 小时前
RAG 每日一技(四):让AI读懂你的话,初探RAG的“灵魂”——Embedding
后端
经典19922 小时前
spring boot 详解以及原理
java·spring boot·后端
Aurora_NeAr2 小时前
Apache Iceberg数据湖高级特性及性能调优
大数据·后端
程序员清风2 小时前
程序员要在你能挣钱的时候拼命存钱!
后端·面试·程序员