Go语言常见的设计模式有哪些?
MySQL大牛
带你全面剖析与系统梳理数据库(mysql等)知识分享,总结数据库技巧和方法,提升你的技术技能。
公众号
JetBrains全家桶正式版激活码&账号开通授权,支持版本升级
https://web.52shizhan.cn/activity/jump/link
学习与交流:Go语言技术微信群
商务合作加微信:LetsFeng
现在就开始你的Go语言学习之旅吧!人生苦短,let’s Go.
Go语言中常见的设计模式包括:
1 单例模式(Singleton Pattern):确保一个类只有一个实例,并提供一个全局访问点。
示例代码如下:
package main
import "fmt"
type Singleton struct {
data string
}
var instance *Singleton
func GetInstance() *Singleton {
if instance == nil {
instance = &Singleton{data: "singleton instance"}
}
return instance
}
func main() {
singleton1 := GetInstance()
singleton2 := GetInstance()
if singleton1 == singleton2 {
fmt.Println("Both singletons are the same instance")
} else {
fmt.Println("Different instances")
}
}
2 工厂模式(Factory Pattern):通过工厂方法创建对象,而无需指定创建对象的具体类。
示例代码如下:
package main
import "fmt"
type Product interface {
GetInfo() string
}
type ConcreteProduct1 struct{}
func (p *ConcreteProduct1) GetInfo() string {
return "Product 1"
}
type ConcreteProduct2 struct{}
func (p *ConcreteProduct2) GetInfo() string {
return "Product 2"
}
type Factory struct{}
func (f *Factory) CreateProduct(productType int) Product {
switch productType {
case 1:
return &ConcreteProduct1{}
case 2:
return &ConcreteProduct2{}
default:
return nil
}
}
func main() {
factory := &Factory{}
product1 := factory.CreateProduct(1)
product2 := factory.CreateProduct(2)
fmt.Println(product1.GetInfo())
fmt.Println(product2.GetInfo())
}
3 装饰器模式(Decorator Pattern):允许向现有对象添加新功能,同时保持其结构不变。
示例代码如下:
package main
import "fmt"
type Component interface {
Operation() string
}
type ConcreteComponent struct{}
func (c *ConcreteComponent) Operation() string {
return "Concrete Component"
}
type Decorator struct {
component Component
}
func (d *Decorator) Operation() string {
return "Decorator " + d.component.Operation()
}
func main() {
component := &ConcreteComponent{}
decorator := &Decorator{component: component}
fmt.Println(component.Operation())
fmt.Println(decorator.Operation())
}
4 观察者模式(Observer Pattern):定义了对象之间的一对多依赖关系,使得当一个对象改变状态时,其所有依赖对象都会收到通知并自动更新。
示例代码如下:
package main
import "fmt"
type Observer interface {
Update(string)
}
type Subject struct {
observers []Observer
state string
}
func (s *Subject) Attach(observer Observer) {
s.observers = append(s.observers, observer)
}
func (s *Subject) SetState(state string) {
s.state = state
s.Notify()
}
func (s *Subject) Notify() {
for _, observer := range s.observers {
observer.Update(s.state)
}
}
type ConcreteObserver struct {
name string
}
func (co *ConcreteObserver) Update(state string) {
fmt.Printf("Observer %s received the state: %s\n", co.name, state)
}
func main() {
subject := &Subject{}
observer1 := &ConcreteObserver{name: "Observer 1"}
observer2 := &ConcreteObserver{name: "Observer 2"}
subject.Attach(observer1)
subject.Attach(observer2)
subject.SetState("new state")
}
这些是在Go语言中常见的几种设计模式,它们可以帮助开发人员组织代码并解决特定的设计问题。在实际开发中,选择适当的设计模式取决于应用程序的需求和架构。
文章首发:
更多相关Go语言的技术文章或视频教程,请关注本公众号获取并查看,感谢你的支持与信任!
收录于合集 #学Go语言哪些事儿
229个上一篇用 Go语言 在处理队列时,redis如何做生产与消费订单任务的。守护进程又是如何写?下一篇Go语言 如何在终端打开 实现进度条处理 数据
微信扫一扫
关注该公众号
分类:
GO
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2022-10-27 Go的方法接收者:值接收者与指针接收者
2021-10-27 Python实现目录文件的全量和增量备份
2021-10-27 nfs卸载目录报错
2021-10-27 kubernetes用户权限管理工具permission-manager