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函数和完整代码示例。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐