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

文章目录

  • [1. 概念](#1. 概念)
    • [1.1 角色](#1.1 角色)
    • [1.2 类图](#1.2 类图)
  • [2. 代码示例](#2. 代码示例)
    • [2.1 设计](#2.1 设计)
    • [2.1 代码](#2.1 代码)
    • [2.2 类图](#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 代码

go 复制代码
package main

import "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 Color
	fmt.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()
}
  • 输出
shell 复制代码
========== 第一次测试 ==========
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

相关推荐
007php0074 小时前
某游戏互联网大厂Java面试深度解析:Java基础与性能优化(一)
java·数据库·面试·职场和发展·性能优化·golang·php
安冬的码畜日常5 小时前
【JUnit实战3_27】第十六章:用 JUnit 测试 Spring 应用:通过实战案例深入理解 IoC 原理
spring·观察者模式·设计模式·单元测试·ioc·依赖注入·junit5
她说彩礼65万6 小时前
C#设计模式 单例模式实现方式
单例模式·设计模式·c#
安冬的码畜日常10 小时前
【JUnit实战3_28】第十七章:用 JUnit 5 实测 SpringBoot 项目
spring boot·功能测试·测试工具·设计模式·单元测试·junit5
围巾哥萧尘10 小时前
TRAE Agent 歌曲创作助手构建与使用教程🧣
设计模式
superman超哥11 小时前
仓颉语言中流式I/O的设计模式深度剖析
开发语言·后端·设计模式·仓颉
豆浆whisky11 小时前
Go内存管理最佳实践:提升性能的Do‘s与Don‘ts|Go语言进阶(17)
开发语言·后端·golang
m0_7482480212 小时前
Spring设计模式刨根问底
java·spring·设计模式
不要额外加糖14 小时前
tql,寥寥几行,实现无队列无感刷新
前端·javascript·设计模式
ACGkaka_14 小时前
设计模式学习(十二)状态模式
学习·设计模式·状态模式