设计模式是面向对象编程中的常用概念,它可以提高代码的复用性、可读性和可维护性。Golang 作为一门较新的编程语言,不仅在高并发、网络编程等方面表现优异,而且对设计模式的支持也非常完善。本文将系统介绍 Golang 中常用的设计模式,希望能对广大程序员有所帮助。

一、单例模式

单例模式(Singleton Pattern)被广泛应用于日志、配置等资源的获取。其定义如下:

确保一个类只有一个实例,并提供一个全局访问点。

在 Golang 中,常用的单例模式实现如下:

type singleton struct {}

var instance *singleton

func GetInstance() *singleton {
    if instance == nil {
        instance = &singleton{}
    }
    return instance
}
GetInstance()
var (
    instance *singleton
    once     sync.Once
)

func GetInstance() *singleton {
    once.Do(func() {
        instance = &singleton{}
    })
    return instance
}
sync.OnceGetInstance()

二、工厂模式

工厂模式(Factory Pattern)是将对象创建的过程封装起来,从而简化客户端的代码。其定义如下:

定义一个用于创建对象的接口,让子类决定实例化哪一个类。

在 Golang 中,常用的工厂模式实现如下(简单工厂模式):

type Animal interface {
    Speak()
}

type Dog struct{}

func (d Dog) Speak() {
    fmt.Println("汪汪汪")
}

type Cat struct{}

func (c Cat) Speak() {
    fmt.Println("喵喵喵")
}

type AnimalFactory struct{}

func (f AnimalFactory) CreateAnimal(animalType string) Animal {
    switch animalType {
    case "dog":
        return Dog{}
    case "cat":
        return Cat{}
    default:
        return nil
    }
}
AnimalDogCatAnimalFactoryAnimalCreateAnimal()animalType

三、适配器模式

适配器模式(Adapter Pattern)是将一个接口转换成客户端希望的另一个接口。其定义如下:

将一个类的接口转换成客户希望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

在 Golang 中,常用的适配器模式实现如下:

type Target interface {
    Request()
}

type Adaptee struct{}

func (a Adaptee) SpecificRequest() {
    fmt.Println("适配器模式:使用 Adaptee 的方法")
}

type Adapter struct {
    Adaptee
}

func (a Adapter) Request() {
    a.SpecificRequest()
}
TargetAdapteeAdapteeSpecificRequest()TargetAdapterTargetAdapteeRequest()SpecificRequest()

四、装饰器模式

装饰器模式(Decorator Pattern)是在不改变原有对象的前提下,给对象添加新的功能。其定义如下:

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

在 Golang 中,常用的装饰器模式实现如下:

type Component interface {
    Operation()
}

type ConcreteComponent struct{}

func (c ConcreteComponent) Operation() {
    fmt.Println("原始操作")
}

type Decorator struct {
    Component
}

func (d Decorator) Operation() {
    d.Component.Operation()
}

type ConcreteDecoratorA struct {
    Decorator
}

func (d ConcreteDecoratorA) Operation() {
    d.Decorator.Operation()
    fmt.Println("装饰器 A 的操作")
}

type ConcreteDecoratorB struct {
    Decorator
}

func (d ConcreteDecoratorB) Operation() {
    d.Decorator.Operation()
    fmt.Println("装饰器 B 的操作")
}
ComponentConcreteComponentDecoratorComponentComponentOperation()ComponentConcreteDecoratorAConcreteDecoratorBDecorator

五、观察者模式

观察者模式(Observer Pattern)是对象间的一种一对多的依赖关系,当对象状态发生改变时,所有依赖它的对象都会得到通知。其定义如下:

定义对象间的一种一对多的依赖关系,使得当一个对象状态发生改变时,所有依赖它的对象都得到通知并自动更新。

在 Golang 中,常用的观察者模式实现如下:

type Observer interface {
    Update(string)
}

type Subject interface {
    Attach(Observer)
    Detach(Observer)
    Notify(string)
}

type ConcreteObserver struct {
    Name string
}

func (o ConcreteObserver) Update(msg string) {
    fmt.Printf("%s 收到消息:%s\n", o.Name, msg)
}

type ConcreteSubject struct {
    Observers []Observer
}

func (s *ConcreteSubject) Attach(observer Observer) {
    s.Observers = append(s.Observers, observer)
}

func (s *ConcreteSubject) Detach(observer Observer) {
    for i, o := range s.Observers {
        if o == observer {
            s.Observers = append(s.Observers[:i], s.Observers[i+1:]...)
            break
        }
    }
}

func (s *ConcreteSubject) Notify(msg string) {
    for _, observer := range s.Observers {
        observer.Update(msg)
    }
}
ObserverSubjectConcreteObserverObserverConcreteSubjectSubjectNotify()Update()

总结

本文介绍了 Golang 中常用的几种设计模式,并提供了实现代码。单例模式可以确保一个类只有一个实例;工厂模式可以简化对象创建的过程;适配器模式可以将一个接口转换成另一个接口;装饰器模式可以在不改变原有对象的前提下,给对象添加新的功能;观察者模式可以实现对象间的一种一对多的依赖关系。这些设计模式可以提高代码的复用性、可读性和可维护性,值得广大程序员认真掌握和使用。