Go语言设计模式实战_golang常用模式教程

go语言设计模式是利用go特性解决常见软件设计问题的方法,其核心在于结合go简洁语法和并发能力。1. 创建型模式如单例通过sync.once实现,工厂通过接口与函数实现,建造者通过结构体链式方法构建复杂对象;2. 结构型模式如适配器用组合转换接口,装饰器动态添加职责,外观封装复杂逻辑提供统一入口;3. 行为型模式如观察者用channel和goroutine实现通知机制,策略通过接口封装算法,模板方法用匿名函数定义执行骨架。go中使用设计模式应适度,避免过度复杂化代码,优先采用简单直接的“go式”解决方案。

Go语言设计模式实战_golang常用模式教程

go语言设计模式,说白了,就是用Go的特性,把一些常见的软件设计问题给漂亮地解决了。与其说是“设计模式”,不如说是“Go式解决问题”,更贴切。

Go语言设计模式实战_golang常用模式教程

解决方案

Go语言设计模式实战_golang常用模式教程

Go的设计模式,其实很大程度上受益于Go本身简洁的语法和强大的并发能力。比如,单例模式在Go里实现起来就比Java简单得多,因为Go的sync.Once天生就是为这个设计的。

立即学习go语言免费学习笔记(深入)”;

Go语言设计模式实战_golang常用模式教程

  • 创建型模式: 这类模式主要解决对象创建的问题,让你能更灵活地控制对象的生成过程。

    • 单例模式 (Singleton): 确保一个类只有一个实例,并提供一个全局访问点。Go里用sync.Once就能轻松实现。
    package singleton  import "sync"  type singleton struct { }  var instance *singleton var once sync.Once  func GetInstance() *singleton {     once.Do(func() {         instance = &singleton{}     })     return instance }
    • 工厂模式 (Factory): 定义一个创建对象的接口,让子类决定实例化哪个类。Go里可以用接口和函数来实现。
    package factory  type Animal interface {     speak() string }  type Dog struct{}  func (d *Dog) Speak() string {     return "Woof!" }  type Cat struct{}  func (c *Cat) Speak() string {     return "Meow!" }  func NewAnimal(animalType string) Animal {     switch animalType {     case "dog":         return &Dog{}     case "cat":         return &Cat{}     default:         return nil     } }
    • 建造者模式 (Builder): 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。Go里可以定义一个Builder接口,然后实现不同的Builder。
    package builder  type House struct {     Walls  int     Doors  int     windows int     Garage bool }  type HouseBuilder interface {     SetWalls(int) HouseBuilder     SetDoors(int) HouseBuilder     SetWindows(int) HouseBuilder     SetGarage(bool) HouseBuilder     Build() House }  type ConcreteHouseBuilder struct {     walls  int     doors  int     windows int     garage bool }  func (b *ConcreteHouseBuilder) SetWalls(walls int) HouseBuilder {     b.walls = walls     return b }  func (b *ConcreteHouseBuilder) SetDoors(doors int) HouseBuilder {     b.doors = doors     return b }  func (b *ConcreteHouseBuilder) SetWindows(windows int) HouseBuilder {     b.windows = windows     return b }  func (b *ConcreteHouseBuilder) SetGarage(garage bool) HouseBuilder {     b.garage = garage     return b }  func (b *ConcreteHouseBuilder) Build() House {     return House{         Walls:  b.walls,         Doors:  b.doors,         Windows: b.windows,         Garage: b.garage,     } }
  • 结构型模式: 这类模式关注如何组合对象,形成更大的结构。

    • 适配器模式 (Adapter): 将一个类的接口转换成客户希望的另外一个接口。Go里用组合来实现。
    package adapter  type LegacyPrinter interface {     Print(string) string }  type MyLegacyPrinter struct{}  func (l *MyLegacyPrinter) Print(s string) string {     return "Legacy Printer: " + s }  type ModernPrinter interface {     PrintStored() string }  type PrinterAdapter struct {     LegacyPrinter LegacyPrinter     Msg string }  func (p *PrinterAdapter) PrintStored() string {     if p.LegacyPrinter != nil {         return p.LegacyPrinter.Print(p.Msg)     }     return p.Msg }
    • 装饰器模式 (Decorator): 动态地给一个对象添加一些额外的职责。Go里用组合和接口来实现。
    package decorator  type Component interface {     Operation() string }  type ConcreteComponent struct{}  func (c *ConcreteComponent) Operation() string {     return "ConcreteComponent" }  type Decorator struct {     Component Component }  func (d *Decorator) Operation() string {     return d.Component.Operation() }  type ConcreteDecoratorA struct {     Decorator }  func (d *ConcreteDecoratorA) Operation() string {     return "ConcreteDecoratorA(" + d.Decorator.Operation() + ")" }  type ConcreteDecoratorB struct {     Decorator }  func (d *ConcreteDecoratorB) Operation() string {     return "ConcreteDecoratorB(" + d.Decorator.Operation() + ")" }
    • 外观模式 (Facade): 为子系统中的一组接口提供一个统一的入口。Go里就是创建一个简单的结构体,封装复杂的逻辑。
    package facade  type CPU struct{}  func (c *CPU) Start() {     // ... }  type Memory struct{}  func (m *Memory) Load() {     // ... }  type HardDrive struct{}  func (h *HardDrive) ReadData() {     // ... }  type ComputerFacade struct {     cpu CPU     memory Memory     hardDrive HardDrive }  func (c *ComputerFacade) Start() {     c.cpu.Start()     c.memory.Load()     c.hardDrive.ReadData() }
  • 行为型模式: 这类模式关注对象之间的职责分配和算法。

    • 观察者模式 (Observer): 定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。Go里用channel和goroutine来实现。
    package observer  type Observer interface {     Update(string) }  type Subject interface {     Attach(Observer)     Detach(Observer)     Notify(string) }  type ConcreteSubject struct {     observers []Observer }  func (s *ConcreteSubject) Attach(observer Observer) {     s.observers = append(s.observers, observer) }  func (s *ConcreteSubject) Detach(observer Observer) {     // Implementation to remove observer }  func (s *ConcreteSubject) Notify(message string) {     for _, observer := range s.observers {         observer.Update(message)     } }  type ConcreteObserver struct {     name string }  func (o *ConcreteObserver) Update(message string) {     // ... }
    • 策略模式 (Strategy): 定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换。Go里用接口来实现。
    package strategy  type Strategy interface {     Execute(int, int) int }  type AddStrategy struct{}  func (a *AddStrategy) Execute(a1, a2 int) int {     return a1 + a2 }  type SubtractStrategy struct{}  func (s *SubtractStrategy) Execute(a1, a2 int) int {     return a1 - a2 }  type Context struct {     strategy Strategy }  func (c *Context) SetStrategy(strategy Strategy) {     c.strategy = strategy }  func (c *Context) ExecuteStrategy(a1, a2 int) int {     return c.strategy.Execute(a1, a2) }
    • 模板方法模式 (Template Method): 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Go里可以用匿名函数来实现,虽然和传统OO语言的模板方法略有不同。
    package template  type Template interface {     Step1()     Step2()     Hook() }  type ConcreteTemplate struct {     Template }  func (c *ConcreteTemplate) Step1() {     // ... }  func (c *ConcreteTemplate) Step2() {     // ... }  func (c *ConcreteTemplate) Hook() {     // Optional hook }  func ExecuteTemplate(t Template) {     t.Step1()     t.Step2()     t.Hook() }

为什么要在Go中使用设计模式?

说实话,Go本身的设计哲学就是简单直接。很多时候,过度使用设计模式反而会适得其反,让代码变得复杂难懂。但是,在面对复杂系统时,合理地运用设计模式,可以提高代码的可维护性、可扩展性和可重用性。而且,理解设计模式,能让你更好地理解和使用现有的Go库。

Go语言并发模式与设计模式有什么关系?

Go的并发模型,例如goroutine和channel,本身就可以看作是一种特殊的设计模式。例如,使用channel来实现生产者-消费者模式,或者使用select来实现多路复用。这些并发模式,可以和传统的设计模式结合使用,构建出更健壮、更高效的系统。

如何避免在Go语言中过度设计?

这是个好问题。关键在于“适度”。在开始设计之前,先问问自己:这个问题真的需要用设计模式来解决吗?有没有更简单、更直接的Go式方法?如果答案是否定的,那就不要犹豫,直接用最简单的代码实现。记住,代码是写给人看的,其次才是给机器执行的。过度设计的代码,只会增加维护成本。

以上就是Go语言设计模式实战_<a

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享