当前位置: 首页 > news >正文

《golang设计模式》第二部分·结构型模式-02-桥接模式(Bridge)

文章目录

  • 1. 概念
    • 1.1 角色
    • 1.2 类图
  • 2. 代码示例
    • 2.1 设计
    • 2.1 代码
    • 2.2 类图

1. 概念

客户端调用桥接接口实现原有功能和扩展功能的组合

1.1 角色

  • Implementor(实施者):
    • 具体实施者的抽象,可以是一个接口。
  • Concrete Implementor(具体实施者):
    • 可以理解为扩展之前的原有功能
    • 桥接接口会在实现扩展功能的基础上调用它实现这些原有功能
  • Abstraction(抽象化):
    • 我们可以理解为桥接接口,它在提供扩展功能的同时也桥接了原有功能的接口
    • Refined Abstraction提供一个统一接口
    • 它关联了Implementor(或者进一步是Implementor的聚合)
  • Refined Abstraction(扩展抽象化):
    • 可以理解为它实现了具体的扩展功能,并实际调用了mplementor接口完成了原有功能

1.2 类图

«interface»
Implementor
+serviceImpl() : ReturnType
ConcreteImplementor
+serviceImpl() : ReturnType
+customService() : Type
«interface»
Abstraction
+Implementor:Implementor
+abstractService()
RefinedAbstraction
+extraService()
Client

2. 代码示例

2.1 设计

  • 定义一个实施者颜色
  • 定义三个具体实施者红色绿色黄色
    • 他们的use()方法来实现使用对应颜色
  • 定义一个抽象化类(桥接接口)笔刷
  • 定义两个扩展抽象化类粗笔刷细笔刷
    • 他们的画画方法
      • 实现扩展功能——用对应笔刷画画
      • 同时调用实施者接口,实现了对应的颜色功能
  • 定义一个工厂函数用来实例化一个具体的笔刷
  • 调用
    • 声明一个实施者
    • 实例化一个具体实施者
    • 用具体实施者实例化一个桥接
    • 调用桥接的方法实现原有功能和扩展功能的组合

2.1 代码

package mainimport "fmt"//定义实施者类
type Color interface {Use()
}//定义具体实施者A
type Red struct{}func (r Red) Use() {fmt.Println("Use Red color")
}
//定义具体实施者B
type Green struct{}func (g Green) Use() {fmt.Println("Use Green color")
}
//定义具体实施者C
type Yellow struct{}func (y Yellow) Use() {fmt.Println("Use Yellow color")
}// 定义抽象化类(或叫桥接接口)
type BrushPen interface {DrawPicture()
}// 定义扩展抽象化A
type BigBrushPen struct {Color
}//提供扩展功能,同时选择原功能执行
func (bbp BigBrushPen) DrawPicture() {fmt.Println("Draw picture with big brush pen")bbp.Use()
}// 定义扩展抽象化B
type SmallBrushPen struct {Color
}
//提供扩展功能,同时选择原功能执行
func (sbp SmallBrushPen) DrawPicture() {fmt.Println("Draw picture with small brush pen")sbp.Use()
}// 定义工厂方法生产具体的扩展抽象化(此处为了方便展示,和桥接模式无关)
func NewBrushPen(t string, color Color) BrushPen {switch t {case "BIG":return BigBrushPen{Color: color,}case "SMALL":return SmallBrushPen{Color: color,}default:return nil}
}func main() {//声明实施者var tColor Colorfmt.Println("========== 第一次测试 ==========")//定义为具体实施者tColor = Red{}//用具体实施者实例化一个抽象化tBrushPen := NewBrushPen("BIG", tColor)//用抽象化的画画功能完成扩展功能(粗细笔刷)和对应原功能(颜色)的组合操作tBrushPen.DrawPicture()fmt.Println("========== 第二次测试 ==========")tColor = Green{}tBrushPen = NewBrushPen("SMALL", tColor)tBrushPen.DrawPicture()fmt.Println("========== 第三次测试 ==========")tColor = Yellow{}tBrushPen = NewBrushPen("BIG", tColor)tBrushPen.DrawPicture()
}
  • 输出
========== 第一次测试 ==========
Draw picture with big brush pen  
Use Red color
========== 第二次测试 ========== 
Draw picture with small brush pen
Use Green color
========== 第三次测试 ========== 
Draw picture with big brush pen  
Use Yellow color

2.2 类图

«interface»
Color
+Use()
Red
+Use()
Green
+Use()
Yellow
+Use()
«interface»
BrushPen
+DrawPicture()
BigBrushPen
+Color
+DrawPicture()
SmallBrushPen
+Color
+DrawPicture()
Client
http://www.lryc.cn/news/122819.html

相关文章:

  • 【2023年11月第四版教材】《第4章-信息系统管理之管理要点(第四版新增章节)(第二部分)》
  • 【算法——双指针】LeetCode 1089 复写零
  • 基于飞桨图学习框架实现的城市地点动态关系挖掘
  • 3.1 Qt样式选择器
  • react钩子副作用理解
  • 浅谈Spring与字节码生成技术
  • 时序预测 | MATLAB实现基于BiLSTM双向长短期记忆神经网络的时间序列预测-递归预测未来(多指标评价)
  • Flink多流处理之coGroup(协同分组)
  • 基于TICK的DevOps监控实战(Ubuntu20.04系统,Telegraf+InfluDB+Chronograf+Kapacitor)
  • 十九、docker学习-Dockerfile
  • Docker容器的数据卷
  • 推荐工具!使终端便于 DevOps 和 Kubernetes 使用
  • 抖音小程序实现less语言编译样式
  • 介绍 TensorFlow 的基本概念和使用场景
  • 抖音关键词搜索小程序排名怎么做
  • Windows下升级jdk1.8小版本
  • [保研/考研机试] KY235 进制转换2 清华大学复试上机题 C++实现
  • 机器学习 | Python实现KNN(K近邻)模型实践
  • Mybatis 源码 ③ :SqlSession
  • Python 潮流周刊#15:如何分析异步任务的性能?
  • 二叉搜索树K和KV结构模拟
  • nlohmann json:检查object是否存在某个键
  • 15-1_Qt 5.9 C++开发指南_Qt多媒体模块概述
  • 分页查询中起始位置的计算
  • Failed to execute goal org.apache.maven.plugins
  • 50吨收费站生活一体化污水处理设备厂家价格低
  • UG NX二次开发(C#)-CAM-获取刀具类型
  • Flask 框架集成Bootstrap
  • 在k8s 1.26.6上部署ES集群
  • 用神经网络玩转数据聚类:自编码器的原理与实践