go-pattern-examples icon indicating copy to clipboard operation
go-pattern-examples copied to clipboard

Examples of implement for awesome go patterns including usual design patterns, in easy understanding examples.

Golang工程模式示例集合(Go Patterns Examples)

包括了go-patternsgolang-design-pattern中的全部模式

目前包括42种Go中常用的、面向工程化和最佳实践的模式/套路,自然也包含常见的23种设计模式,重点是这里全部是例子、通俗易懂,甚至每个模式下的例子,改一下名字,稍微再增加几行代码就可以直接用在项目和工程中了。

每一种设计模式都有其特定的应用场景和要解决的问题,了解模式的关键点就在于弄清这些目标场景和问题,千万不要纠结于:为什么这个模式叫这个名字,这个模式为啥要这样用? 要知道,最初,这些模式不是你总结的,也不是我的总结的,命名不由你我,如果是你我的写的,当然可以按照自己的喜欢的感觉给这些套路取名字,让其他人去费劲想

了解一下姿势 Ways

  • 所谓模式就是套路,如功夫,招有定式
  • 学习模式,就是学习套路,弄清楚套路要解决的目标场景,这很重要.
  • 这里就是以实际代码示例展示设计模式,通俗易懂
  • 除了常见的23种普适的设计模式,Go也有一些属于自己的模式

走起 Go

下载:

go https://github.com/crazybber/go-pattern-examples

跑一遍测试:

cd go-pattern-examples
go test ./...

行为型模式 Behavior Patterns

  • [x] 备忘录模式(Memento)
  • [x] 中介者模式(Mediator)
  • [x] 闭包选项模式(Function Option)
  • [x] 观察者模式(Observer)
  • [x] 命令模式(Command)
  • [x] 迭代器模式(Iterator)
  • [x] 模板方法模式(Template Method)
  • [x] 策略模式(Strategy)
  • [x] 状态模式(State)
  • [x] 访问者模式(Visitor)
  • [x] 解释器模式(Interpreter)
  • [x] 职责链模式(Chain of Responsibility)

创建型模式 Creation Patterns

  • [x] New模式(New)
  • [x] 简单工厂模式(Simple Factory)
  • [x] 创建者模式(Builder)
  • [x] 单例模式(Singleton)
  • [x] 对象池模式(Object Pool)
  • [x] 工厂方法模式(Factory Method)
  • [x] 抽象工厂模式(Abstract Factory)
  • [x] 原型模式(Prototype)

结构型模式 Structure Patterns

  • [x] 外观模式(Facade)
  • [x] 适配器模式(Adapter)
  • [x] 桥模式(Bridge)
  • [x] 复合模式(Composite)
  • [x] 享元模式(Flyweight)
  • [x] 装饰器模式(Decorator)
  • [x] 代理模式(Proxy)

弹性模式 Resiliency Patterns

  • [x] 熔断模式(circuit breaker)
  • [x] 限流模式(rate limiting)
  • [ ] [WIP]重试模式(retrier)
  • [x] 最后期限模式(deadline)

更多模式(同步/并发/并行) Go More Patterns(Concurrency/Parallelism/Sync)

  • [x] 发布订阅模式(Pub-Sub)
  • [x] 时差模式(Time Profile)
  • [x] 上下文模式(Context)
  • [x] 扇入模式(Fan-In)
  • [x] 扇出模式(Fan-Out)
  • [ ] [WIP]信号量模式(Semaphore)
  • [ ] [WIP]并行模式(Parallelism)
  • [ ] [WIP]生成器模式(Generators)
  • [ ] [WIP]屏障模式(N-Barrier)
  • [ ] [WIP]有限并行模式(Bounded Parallelism)
  • [ ] [WIP]批处理模式(batcher)

参考资料(Design patters Articles)

Patterns Instructions Status
go-patterns 搜集整理到各种模式文章和代码 p
菜鸟设计模式 以Java为主的设计模式介绍 p
design_pattern 编程之家的设计模式 p
golang-design-pattern go语言的设计模式理论 p
go-resiliency go的一些弹性模式 m
Behavioral 设计模式中的行为模式(俄语版) m
go-patterns 一些设计模式 p
go_design_pattern 日语版设计模式 p
microsoft-patterns 微软设计模式系列文章(24) p/w

更多 More

需要重新温习下Go基础?看这里

go-fucking-exercises