Go语言常见的设计模式有哪些?

 

磊丰 Go语言圈 2023-10-24 08:30 发表于中国香港
MySQL大牛
带你全面剖析与系统梳理数据库(mysql等)知识分享,总结数据库技巧和方法,提升你的技术技能。
45篇原创内容

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语言 如何在终端打开 实现进度条处理 数据
 
 
 

微信扫一扫
关注该公众号

 
 
posted @ 2023-10-27 21:49  技术颜良  阅读(126)  评论(0编辑  收藏  举报