golang架构师必懂的五种设计模式

Go语言中常见的五种设计模式包括:单例模式、工厂模式、观察者模式、策略模式和装饰器模式。以下是给出每个设计模式对应的main函数和完整代码示例:

  1. 单例模式:
package main

import "fmt"

type Singleton struct{}

var instance *Singleton

func GetInstance() *Singleton {
    if instance == nil {
        instance = &Singleton{}
    }
    return instance
}

func (s *Singleton) showMessage() {
    fmt.Println("Hello, I am a Singleton.")
}

func main() {
    singleton := GetInstance()
    singleton.showMessage()
}
  1. 工厂模式:
package main

import "fmt"

type Product interface {
    showMessage()
}

type ConcreteProductA struct{}

func (p *ConcreteProductA) showMessage() {
    fmt.Println("Hello, I am Product A.")
}

type ConcreteProductB struct{}

func (p *ConcreteProductB) showMessage() {
    fmt.Println("Hello, I am Product B.")
}

type Factory struct{}

func (f *Factory) createProduct(productType int) Product {
    switch productType {
    case 1:
        return &ConcreteProductA{}
    case 2:
        return &ConcreteProductB{}
    default:
        return nil
    }
}

func main() {
    factory := &Factory{}

    productA := factory.createProduct(1)
    productA.showMessage()

    productB := factory.createProduct(2)
    productB.showMessage()
}
  1. 观察者模式:
package main

import "fmt"

type Observer interface {
    update(data int)
}

type ConcreteObserverA struct{}

func (o *ConcreteObserverA) update(data int) {
    fmt.Println("Observer A:", data)
}

type ConcreteObserverB struct{}

func (o *ConcreteObserverB) update(data int) {
    fmt.Println("Observer B:", data)
}

type Subject struct {
    observers []Observer
}

func (s *Subject) attach(observer Observer) {
    s.observers = append(s.observers, observer)
}

func (s *Subject) setData(value int) {
    for _, observer := range s.observers {
        observer.update(value)
    }
}

func main() {
    subject := &Subject{}
    observerA := &ConcreteObserverA{}
    observerB := &ConcreteObserverB{}

    subject.attach(observerA)
    subject.attach(observerB)

    subject.setData(10)
}
  1. 策略模式:
package main

import "fmt"

type Strategy interface {
    execute()
}

type ConcreteStrategyA struct{}

func (s *ConcreteStrategyA) execute() {
    fmt.Println("Executing Strategy A.")
}

type ConcreteStrategyB struct{}

func (s *ConcreteStrategyB) execute() {
    fmt.Println("Executing Strategy B.")
}

type Context struct {
    strategy Strategy
}

func (c *Context) setStrategy(strategy Strategy) {
    c.strategy = strategy
}

func (c *Context) executeStrategy() {
    c.strategy.execute()
}

func main() {
    context := &Context{}
    strategyA := &ConcreteStrategyA{}
    strategyB := &ConcreteStrategyB{}

    context.setStrategy(strategyA)
    context.executeStrategy()

    context.setStrategy(strategyB)
    context.executeStrategy()
}
  1. 装饰器模式:
package main

import "fmt"

type Component interface {
    showMessage()
}

type ConcreteComponent struct{}

func (c *ConcreteComponent) showMessage() {
    fmt.Println("Hello, I am a Concrete Component.")
}

type Decorator struct {
    component Component
}

func (d *Decorator) showMessage() {
    d.component.showMessage()
}

type ConcreteDecoratorA struct {
    Decorator
}

func (d *ConcreteDecoratorA) showMessage() {
    d.Decorator.showMessage()
    fmt.Println("Additional functionality A.")
}

type ConcreteDecoratorB struct {
    Decorator
}

func (d *ConcreteDecoratorB) showMessage() {
    d.Decorator.showMessage()
    fmt.Println("Additional functionality B.")
}

func main() {
    component := &ConcreteComponent{}
    decoratorA := &ConcreteDecoratorA{Decorator: Decorator{component: component}}
    decoratorB := &ConcreteDecoratorB{Decorator: Decorator{component: decoratorA}}

    decoratorB.showMessage()
}

以上是Go语言中常见的五种设计模式,并分别给出了每个设计模式对应的main函数和完整代码示例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超维Ai编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值