第 42 章 - Go语言 设计模式
在Go语言中,设计模式是一种被广泛接受的解决常见问题的最佳实践。这些模式可以分为三类:创建型模式、结构型模式和行为型模式。下面我将结合案例以及源代码对这三种类型的设计模式进行详细讲解。
创建型模式
创建型模式主要关注对象的创建过程,使系统独立于如何创建、组合和表示对象。
1. 单例模式 (Singleton Pattern)
单例模式确保一个类只有一个实例,并提供一个全局访问点。
package singletonimport "sync"type Singleton struct {// 可以添加其他字段
}var instance *Singleton
var once sync.Oncefunc GetInstance() *Singleton {once.Do(func() {instance = &Singleton{}})return instance
}
2. 工厂方法模式 (Factory Method Pattern)
工厂方法定义了一个创建对象的接口,但让子类决定实例化哪一个类。
package factorytype Product interface {Use()
}type ConcreteProductA struct{}func (p *ConcreteProductA) Use() {fmt.Println("Using Product A")
}type ConcreteProductB struct{}func (p *ConcreteProductB) Use() {fmt.Println("Using Product B")
}type Factory interface {CreateProduct() Product
}type ConcreteFactoryA struct{}func (f *ConcreteFactoryA) CreateProduct() Product {return &ConcreteProductA{}
}type ConcreteFactoryB struct{}func (f *ConcreteFactoryB) CreateProduct() Product {return &ConcreteProductB{}
}
结构型模式
结构型模式涉及如何组合类和对象以形成更大的结构。
1. 适配器模式 (Adapter Pattern)
适配器模式允许不同接口的对象通过适配器一起工作。
package adaptertype Target interface {Request() string
}type Adaptee struct{}func (a *Adaptee) SpecificRequest() string {return "Specific request from Adaptee"
}type Adapter struct {adaptee *Adaptee
}func (a *Adapter) Request() string {return a.adaptee.SpecificRequest()
}
2. 装饰器模式 (Decorator Pattern)
装饰器模式允许动态地给一个对象添加新的功能。
package decoratortype Component interface {Operation() string
}type ConcreteComponent struct{}func (c *ConcreteComponent) Operation() string {return "Base operation"
}type Decorator struct {component Component
}func (d *Decorator) Operation() string {return d.component.Operation() + " with additional functionality"
}
行为型模式
行为型模式关注对象之间的责任分配。
1. 观察者模式 (Observer Pattern)
观察者模式定义了对象间的一对多依赖关系,当一个对象状态改变时,所有依赖它的对象都会得到通知并自动更新。
package observertype Subject interface {Register(Observer)Deregister(Observer)NotifyAll()
}type Observer interface {Update(string)
}type ConcreteSubject struct {observers []Observer
}func (s *ConcreteSubject) Register(o Observer) {s.observers = append(s.observers, o)
}func (s *ConcreteSubject) Deregister(o Observer) {for i, obs := range s.observers {if obs == o {s.observers = append(s.observers[:i], s.observers[i+1:]...)break}}
}func (s *ConcreteSubject) NotifyAll() {for _, observer := range s.observers {observer.Update("New message from subject")}
}type ConcreteObserver struct{}func (o *ConcreteObserver) Update(message string) {fmt.Println("Received:", message)
}
以上是几种常见的设计模式及其在Go语言中的实现示例。每种模式都有其特定的应用场景和优势,在实际开发中合理使用可以帮助提高代码质量和可维护性。
当然,我们可以继续探讨更多设计模式及其在Go语言中的实现。接下来我将介绍几种额外的设计模式。
结构型模式 (续)
3. 组合模式 (Composite Pattern)
组合模式允许你以一致的方式处理单个对象和对象组。
package compositetype Component interface {Operation() string
}type Leaf struct{}func (l *Leaf) Operation() string {return "Leaf operation"
}type Composite struct {children []Component
}func (c *Composite) Add(child Component) {c.children = append(c.children, child)
}func (c *Composite) Remove(child Component) {for i, comp := range c.children {if comp == child {c.children = append(c.children[:i], c.children[i+1:]...)break}}
}func (c *Composite) Operation() string {var result stringfor _, child := range c.children {result += child.Operation()}return result
}
行为型模式 (续)
2. 策略模式 (Strategy Pattern)
策略模式定义了一系列算法,并将每个算法封装起来,使它们可以互换。
package strategytype Strategy interface {AlgorithmInterface()
}type ConcreteStrategyA struct{}func (s *ConcreteStrategyA) AlgorithmInterface() {fmt.Println("Algorithm A")
}type ConcreteStrategyB struct{}func (s *ConcreteStrategyB) AlgorithmInterface() {fmt.Println("Algorithm B")
}type Context struct {strategy Strategy
}func (c *Context) SetStrategy(strategy Strategy) {c.strategy = strategy
}func (c *Context) ExecuteStrategy() {c.strategy.AlgorithmInterface()
}
3. 命令模式 (Command Pattern)
命令模式将请求封装成对象,从而让你可以用不同的请求、队列或者日志来参数化其他对象。
package commandtype Command interface {Execute()
}type Receiver struct{}func (r *Receiver) Action() {fmt.Println("Action performed by receiver")
}type ConcreteCommand struct {receiver *Receiver
}func (c *ConcreteCommand) Execute() {c.receiver.Action()
}type Invoker struct {command Command
}func (i *Invoker) SetCommand(command Command) {i.command = command
}func (i *Invoker) Invoke() {i.command.Execute()
}
这些例子展示了如何使用Go语言实现不同的设计模式。每种模式都有其特定的适用场景,选择合适的模式能够帮助开发者构建更灵活、可扩展且易于维护的应用程序。
4. 状态模式 (State Pattern)
状态模式允许对象在其内部状态改变时改变其行为,看起来就像是改变了它的类一样。
package statetype State interface {Handle(context *Context)
}type Context struct {state State
}func (c *Context) SetState(state State) {c.state = state
}func (c *Context) Request() {c.state.Handle(c)
}type ConcreteStateA struct{}func (s *ConcreteStateA) Handle(context *Context) {fmt.Println("Handling in State A")// 过渡到另一个状态context.SetState(&ConcreteStateB{})
}type ConcreteStateB struct{}func (s *ConcreteStateB) Handle(context *Context) {fmt.Println("Handling in State B")// 可以过渡回State A或保持不变
}
5. 迭代器模式 (Iterator Pattern)
迭代器模式提供了一种方法来访问一个容器对象中的元素,而无需暴露其底层表示。
package iteratortype Iterator interface {HasNext() boolNext() interface{}
}type Aggregate interface {CreateIterator() Iterator
}type ConcreteAggregate struct {items []interface{}
}func (a *ConcreteAggregate) Add(item interface{}) {a.items = append(a.items, item)
}func (a *ConcreteAggregate) CreateIterator() Iterator {return &ConcreteIterator{items: a.items, index: 0}
}type ConcreteIterator struct {items []interface{}index int
}func (i *ConcreteIterator) HasNext() bool {return i.index < len(i.items)
}func (i *ConcreteIterator) Next() interface{} {if i.HasNext() {item := i.items[i.index]i.index++return item}return nil
}
6. 访问者模式 (Visitor Pattern)
访问者模式让你可以在不修改已有类的情况下添加新的操作。
package visitortype Visitor interface {VisitConcreteElementA(element *ConcreteElementA)VisitConcreteElementB(element *ConcreteElementB)
}type Element interface {Accept(visitor Visitor)
}type ConcreteElementA struct{}func (e *ConcreteElementA) Accept(visitor Visitor) {visitor.VisitConcreteElementA(e)
}type ConcreteElementB struct{}func (e *ConcreteElementB) Accept(visitor Visitor) {visitor.VisitConcreteElementB(e)
}type ConcreteVisitor1 struct{}func (v *ConcreteVisitor1) VisitConcreteElementA(element *ConcreteElementA) {fmt.Println("ConcreteVisitor1 visits ConcreteElementA")
}func (v *ConcreteVisitor1) VisitConcreteElementB(element *ConcreteElementB) {fmt.Println("ConcreteVisitor1 visits ConcreteElementB")
}type ConcreteVisitor2 struct{}func (v *ConcreteVisitor2) VisitConcreteElementA(element *ConcreteElementA) {fmt.Println("ConcreteVisitor2 visits ConcreteElementA")
}func (v *ConcreteVisitor2) VisitConcreteElementB(element *ConcreteElementB) {fmt.Println("ConcreteVisitor2 visits ConcreteElementB")
}
这些例子进一步展示了Go语言中如何使用设计模式来解决特定问题。每一种模式都有其独特的优势和适用场景,选择合适的模式对于构建高效、可维护的软件系统至关重要。
7. 中介者模式 (Mediator Pattern)
中介者模式定义了一个封装了一系列对象交互的对象,使得这些对象不再直接相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
package mediatortype Mediator interface {Notify(sender string, event string)
}type ConcreteMediator struct {colleague1 *Colleague1colleague2 *Colleague2
}func (m *ConcreteMediator) Notify(sender string, event string) {if sender == "Colleague1" && event == "A" {m.colleague2.React()} else if sender == "Colleague2" && event == "B" {m.colleague1.React()}
}type Colleague interface {SetMediator(mediator Mediator)React()
}type Colleague1 struct {mediator Mediator
}func (c *Colleague1) SetMediator(mediator Mediator) {c.mediator = mediator
}func (c *Colleague1) React() {fmt.Println("Colleague1 reacts")c.mediator.Notify("Colleague1", "A")
}type Colleague2 struct {mediator Mediator
}func (c *Colleague2) SetMediator(mediator Mediator) {c.mediator = mediator
}func (c *Colleague2) React() {fmt.Println("Colleague2 reacts")c.mediator.Notify("Colleague2", "B")
}
8. 备忘录模式 (Memento Pattern)
备忘录模式在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后恢复对象到先前的状态。
package mementotype Memento struct {state string
}type Originator struct {state string
}func (o *Originator) CreateMemento() *Memento {return &Memento{state: o.state}
}func (o *Originator) RestoreFromMemento(m *Memento) {o.state = m.state
}func (o *Originator) ChangeState(newState string) {o.state = newState
}func (o *Originator) ShowState() {fmt.Printf("Current State: %s\n", o.state)
}type Caretaker struct {mementos []*Memento
}func (c *Caretaker) AddMemento(m *Memento) {c.mementos = append(c.mementos, m)
}func (c *Caretaker) GetMemento(index int) *Memento {if index < len(c.mementos) {return c.mementos[index]}return nil
}
9. 解释器模式 (Interpreter Pattern)
解释器模式给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
package interpretertype Expression interface {Interpret(context string) bool
}type TerminalExpression struct{}func (te *TerminalExpression) Interpret(context string) bool {return context == "terminal"
}type OrExpression struct {expr1, expr2 Expression
}func (oe *OrExpression) Interpret(context string) bool {return oe.expr1.Interpret(context) || oe.expr2.Interpret(context)
}type AndExpression struct {expr1, expr2 Expression
}func (ae *AndExpression) Interpret(context string) bool {return ae.expr1.Interpret(context) && ae.expr2.Interpret(context)
}
通过上述例子,你可以看到如何在Go语言中应用各种行为型设计模式。每种模式都针对特定的问题提供了优雅的解决方案,有助于提高代码的质量和可维护性。希望这些信息对您有所帮助!
相关文章:
第 42 章 - Go语言 设计模式
在Go语言中,设计模式是一种被广泛接受的解决常见问题的最佳实践。这些模式可以分为三类:创建型模式、结构型模式和行为型模式。下面我将结合案例以及源代码对这三种类型的设计模式进行详细讲解。 创建型模式 创建型模式主要关注对象的创建过程…...
【机器学习】---大语言模型
引言:开启大语言模型的奇幻旅程 近年来,人工智能(AI)领域正在经历一场前所未有的技术革命,而其中最耀眼的明星莫过于大语言模型(Large Language Models, LLMs)。这些模型,犹如现代科…...

挑战用React封装100个组件【002】
项目地址 https://github.com/hismeyy/react-component-100 组件描述 组件适用于需要展示图文信息的场景,比如产品介绍、用户卡片或任何带有标题、描述和可选图片的内容展示 样式展示 代码展示 InfoCard.tsx import ./InfoCard.cssinterface InfoCardProps {t…...

MarkDown-插入图片-图片url地址的生成获取方法
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、url地址是什么二、如何获取图片的url地址1.了解图床2.使用图床获取图片URL地址2.1进入网站后,点击右下角“Select Image.”按钮,即可…...

插值、拟合和回归分析的相关知识
目录 0 序言 1 分段线性插值 2 多项式插值 3 样条插值 4 最小二乘拟合 5 多元线性回归 0 序言 在生产实践和科学研究中,常常有这些问题: 插值问题:由实验或测量得到变量间的一批离散样点,要求得到变量之间的函数关系或得到样点之外的…...

【小白学机器学习42】进行多次抽样,样本的分布参数和总体的分布参数的关系
目录 1 进行多次抽样,样本的分布参数和总体的分布参数的关系 2 样本容量越大,多次抽样的样本的分布参数和总体的分布参数的关系 3 随着样本容量增大,多次抽样均值的 平均值,方差的变化 4 随着样本容量增大,多次抽…...
链动星海 质引未来|中信银行加码科技金融 “接力式”服务助力“新质生产力”释放
11月26日,第二届中国国际供应链促进博览会(以下简称链博会)在北京中国国际展览中心开幕。中信集团以“链动星海 质引未来”为主题,亮相先进制造链展区。此次布展由中信金控主办、中信银行承办,携手中信证券、中信建投证…...
黑马2024AI+JavaWeb开发入门Day02-JS-VUE飞书作业
视频地址:哔哩哔哩 讲义作业飞书地址:飞书 一、作业1 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge">&l…...

云计算基础-期末复习
第一章:云计算概论 一、云计算的定义与特征 1. 定义: 云计算是一种通过网络以按需、可扩展的方式获取计算资源和服务的模式。它将计算资源视为一种公用事业,用户可以根据需求动态获取和释放资源,而无需了解底层基础设施的细节。…...

Java GET请求 请求参数在Body中使用Json格式传参
业务需要调个三方接口 使用GET请求方式 但是!请求参数不在Query中,竟然在Body中,使用Json格式传参 在API调试工具里面可以调通 在java代码里,死活调不通 网上搜了搜,找到一个靠谱的,记录一下 import o…...

AI数据分析工具(一)
Looker Studio(谷歌)-免费 优点 免费使用:对于中小型企业和个人用户来说,没有任何费用压力,可以免费享受到数据可视化和报表创建的功能。与Google服务集成:特别适合使用Google产品生态的企业,…...

go结构体匿名“继承“方法冲突时继承优先顺序
在 Go 语言中,匿名字段(也称为嵌入字段)可以用来实现继承的效果。当你在一个结构体中匿名嵌入另一个结构体时,嵌入结构体的方法会被提升到外部结构体中。这意味着你可以直接通过外部结构体调用嵌入结构体的方法。 如果多个嵌入结…...
【049】基于51单片机语音录放【Proteus仿真+Keil程序+报告+原理图】
☆、设计硬件组成:51单片机最小系统ISD4004语音芯片LM386音频放大器喇叭LCD1602液晶显示按键控制LED灯。 1、本设计采用STC89C51/52、AT89C51/52、AT89S51/52作为主控芯片,LCD1602液晶显示屏实时显示; 2、系统具有两种模式:录音…...

《软件项目管理》期末-复习题及参考答案
(1)赶工一个任务时,你应该关注( C ) A. 尽可能多的任务 B. 非关键任务 C. 加速执行关键路径上的任务 D. 通过成本最低化加速执行任务 (2)下列哪个不是项目管理计划的一部分?&#x…...
milvus 通俗易懂原理
向量值如何生成的 Milvus 是一个开源的向量数据库,专门用于处理高维向量的存储、搜索和分析。向量值本身通常来自于某些机器学习或深度学习模型的输出,尤其是在自然语言处理(NLP)、计算机视觉(CV)、推荐系…...
什么是撞库、拖库和洗库?
“撞库”是黑客通过收集互联网已泄露的用户和密码信息,生成对应的字典表,尝试批量登陆其他网站后,得到一系列可以登录的用户。 很多用户在不同网站使用的是相同的帐号密码,因此黑客可以通过获取用户在A网站的账户从而尝试登录B网…...

安卓-碎片的使用入门
1.碎片(Fragment)是什么 Fragment是依赖于Activity的,不能独立存在的,是Activity界面中的一部分,可理解为模块化的Activity,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用得非常广泛. Fragment不能独立存在,必须…...

华为IPD流程学习之——深入解读123页华为IPD流程体系设计方法论PPT
该方案全面介绍了华为IPD流程体系设计方法论,包括流程体系建设的背景、理念、架构、核心特征、构建模型、与组织和战略的关系、运营机制、数字化转型以及流程管理组织等内容,旨在为企业提供一套系统的流程体系建设指导,以提升运营效率、质量和…...

DriveMLLM:一个专为自动驾驶空间理解任务设计的大规模基准数据集
2024-11-20, 由武汉大学、中国科学院自动化研究所、悉尼科技大学、牛津大学等合创建了DriveMLLM数据集,该数据集是自动驾驶领域首个专为评估多模态大型语言模型(MLLMs)空间理解能力而设计的基准,对于推动自动驾驶技术的…...

高效处理 iOS 应用中的大规模礼物数据:以直播项目为例(1-礼物池)
引言 在现代iOS应用开发中,处理大规模数据是一个常见的挑战。尤其实在直播项目中,礼物面板作为展示用户互动的重要部分,通常需要实时显示海量的礼物数据。这些数据不仅涉及到不同的区域、主播的动态差异,还需要保证高效的加载与渲…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)
CSI-2 协议详细解析 (一) 1. CSI-2层定义(CSI-2 Layer Definitions) 分层结构 :CSI-2协议分为6层: 物理层(PHY Layer) : 定义电气特性、时钟机制和传输介质(导线&#…...
爬虫基础学习day2
# 爬虫设计领域 工商:企查查、天眼查短视频:抖音、快手、西瓜 ---> 飞瓜电商:京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空:抓取所有航空公司价格 ---> 去哪儿自媒体:采集自媒体数据进…...
稳定币的深度剖析与展望
一、引言 在当今数字化浪潮席卷全球的时代,加密货币作为一种新兴的金融现象,正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而,加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下,稳定…...
鸿蒙DevEco Studio HarmonyOS 5跑酷小游戏实现指南
1. 项目概述 本跑酷小游戏基于鸿蒙HarmonyOS 5开发,使用DevEco Studio作为开发工具,采用Java语言实现,包含角色控制、障碍物生成和分数计算系统。 2. 项目结构 /src/main/java/com/example/runner/├── MainAbilitySlice.java // 主界…...

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决
Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决 问题背景 在一个基于 Spring Cloud Gateway WebFlux 构建的微服务项目中,新增了一个本地验证码接口 /code,使用函数式路由(RouterFunction)和 Hutool 的 Circle…...

基于IDIG-GAN的小样本电机轴承故障诊断
目录 🔍 核心问题 一、IDIG-GAN模型原理 1. 整体架构 2. 核心创新点 (1) 梯度归一化(Gradient Normalization) (2) 判别器梯度间隙正则化(Discriminator Gradient Gap Regularization) (3) 自注意力机制(Self-Attention) 3. 完整损失函数 二…...

基于Springboot+Vue的办公管理系统
角色: 管理员、员工 技术: 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能: 该办公管理系统是一个综合性的企业内部管理平台,旨在提升企业运营效率和员工管理水…...
libfmt: 现代C++的格式化工具库介绍与酷炫功能
libfmt: 现代C的格式化工具库介绍与酷炫功能 libfmt 是一个开源的C格式化库,提供了高效、安全的文本格式化功能,是C20中引入的std::format的基础实现。它比传统的printf和iostream更安全、更灵活、性能更好。 基本介绍 主要特点 类型安全:…...

解析“道作为序位生成器”的核心原理
解析“道作为序位生成器”的核心原理 以下完整展开道函数的零点调控机制,重点解析"道作为序位生成器"的核心原理与实现框架: 一、道函数的零点调控机制 1. 道作为序位生成器 道在认知坐标系$(x_{\text{物}}, y_{\text{意}}, z_{\text{文}}…...
CppCon 2015 学习:REFLECTION TECHNIQUES IN C++
关于 Reflection(反射) 这个概念,总结一下: Reflection(反射)是什么? 反射是对类型的自我检查能力(Introspection) 可以查看类的成员变量、成员函数等信息。反射允许枚…...