2020-05-05 09:44:57 +03:00
|
|
|
|
# Golang工程模式示例集合(Go Patterns Examples)
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-05-03 05:52:05 +03:00
|
|
|
|
**包括了[go-patterns](https://github.com/tmrts/go-patterns) 和[golang-design-pattern](https://github.com/senghoo/golang-design-pattern)中的全部模式**
|
2020-05-02 19:27:46 +03:00
|
|
|
|
|
2020-05-08 15:05:51 +03:00
|
|
|
|
目前包括**42种Go中常用的、面向工程化和最佳实践的模式/套路**,自然也包含常见的23种设计模式,重点是这里全部是例子、通俗易懂,甚至每个模式下的例子,改一下名字,稍微再增加几行代码就可以直接用在项目和工程中了。
|
2020-05-02 09:58:55 +03:00
|
|
|
|
|
2020-05-09 07:05:22 +03:00
|
|
|
|
每一种设计模式都有其特定的应用场景和要解决的问题,了解模式的关键点就在于弄清这些目标场景和问题,千万不要纠结于:为什么这个模式叫这个名字,这个模式为啥要这样用? **要知道,最初,这些模式不是你总结的,也不是我的总结的,命名不由你我,如果是你我的写的,当然可以按照自己的喜欢的感觉给这些套路取名字,让其他人去费劲想**
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2023-09-25 19:04:32 +03:00
|
|
|
|
## 了解姿势 Ways
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-04-22 04:50:47 +03:00
|
|
|
|
+ 所谓模式就是套路,如功夫,招有定式
|
2022-09-25 11:06:17 +03:00
|
|
|
|
+ 学习模式,就是学习一些经典套路,更弄清楚套路要解决的目标场景,这很重要,才能举一反三更好创新
|
|
|
|
|
+ 这里就是以实际代码示例出发,展示设计模式,通俗易懂
|
2020-04-22 04:50:47 +03:00
|
|
|
|
+ 除了常见的23种普适的设计模式,Go也有一些属于自己的模式
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-05-09 07:05:22 +03:00
|
|
|
|
## 走起 Go
|
|
|
|
|
|
|
|
|
|
下载:
|
|
|
|
|
|
2020-05-11 09:28:53 +03:00
|
|
|
|
```bash
|
2020-08-26 11:56:16 +03:00
|
|
|
|
go https://github.com/crazybber/go-pattern-examples
|
2020-05-09 07:05:22 +03:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
跑一遍测试:
|
|
|
|
|
|
2020-05-11 09:28:53 +03:00
|
|
|
|
```bash
|
2020-08-26 11:56:16 +03:00
|
|
|
|
cd go-pattern-examples
|
2020-05-09 07:05:22 +03:00
|
|
|
|
go test ./...
|
|
|
|
|
```
|
|
|
|
|
|
2020-05-05 09:44:57 +03:00
|
|
|
|
## 行为型模式 Behavior Patterns
|
2020-05-02 21:53:02 +03:00
|
|
|
|
|
2020-05-05 17:04:16 +03:00
|
|
|
|
+ [x] [备忘录模式(Memento)](./behavior/09_memento)
|
2020-05-02 21:53:02 +03:00
|
|
|
|
+ [x] [中介者模式(Mediator)](./behavior/01_mediator)
|
|
|
|
|
+ [x] [闭包选项模式(Function Option)](./behavior/02_option)
|
2020-05-03 20:10:21 +03:00
|
|
|
|
+ [x] [观察者模式(Observer)](./behavior/03_observer)
|
2020-05-05 17:24:57 +03:00
|
|
|
|
+ [x] [命令模式(Command)](./behavior/11_command)
|
2020-05-03 14:33:29 +03:00
|
|
|
|
+ [x] [迭代器模式(Iterator)](./behavior/04_iterator)
|
2020-05-03 20:10:21 +03:00
|
|
|
|
+ [x] [模板方法模式(Template Method)](./behavior/05_template_method)
|
2020-05-02 21:53:02 +03:00
|
|
|
|
+ [x] [策略模式(Strategy)](./behavior/12_strategy)
|
2020-05-06 12:12:28 +03:00
|
|
|
|
+ [x] [状态模式(State)](./behavior/behavior16_state)
|
2020-05-04 18:05:17 +03:00
|
|
|
|
+ [x] [访问者模式(Visitor)](./behavior/07_visitor)
|
2020-05-05 13:44:47 +03:00
|
|
|
|
+ [x] [解释器模式(Interpreter)](./behavior/08_interpreter)
|
2020-05-04 19:02:06 +03:00
|
|
|
|
+ [x] [职责链模式(Chain of Responsibility)](./behavior/06_chain_of_responsibility)
|
2020-05-02 21:53:02 +03:00
|
|
|
|
|
2020-05-05 09:44:57 +03:00
|
|
|
|
## 创建型模式 Creation Patterns
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-05-02 12:36:50 +03:00
|
|
|
|
+ [x] [New模式(New)](./creation/01_new)
|
|
|
|
|
+ [x] [简单工厂模式(Simple Factory)](./creation/02_simple_factory)
|
|
|
|
|
+ [x] [创建者模式(Builder)](./creation/03_builder)
|
|
|
|
|
+ [x] [单例模式(Singleton)](./creation/06_singleton)
|
2020-05-05 17:59:28 +03:00
|
|
|
|
+ [x] [对象池模式(Object Pool)](./creation/04_object_pool)
|
2020-05-02 12:36:50 +03:00
|
|
|
|
+ [x] [工厂方法模式(Factory Method)](./creation/05_factory_method)
|
|
|
|
|
+ [x] [抽象工厂模式(Abstract Factory)](./creation/08_abstract_factory)
|
2020-04-30 12:50:31 +03:00
|
|
|
|
+ [x] [原型模式(Prototype)](./creation/07_prototype)
|
2020-05-02 12:36:50 +03:00
|
|
|
|
|
2020-05-05 09:44:57 +03:00
|
|
|
|
## 结构型模式 Structure Patterns
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-04-30 12:50:31 +03:00
|
|
|
|
+ [x] [外观模式(Facade)](./structure/01_facade)
|
|
|
|
|
+ [x] [适配器模式(Adapter)](./structure/02_adapter)
|
2020-05-02 12:40:51 +03:00
|
|
|
|
+ [x] [桥模式(Bridge)](./structure/03_bridge)
|
|
|
|
|
+ [x] [复合模式(Composite)](./structure/05_composite)
|
|
|
|
|
+ [x] [享元模式(Flyweight)](./structure/04_flyweight)
|
2020-05-02 13:25:12 +03:00
|
|
|
|
+ [x] [装饰器模式(Decorator)](./structure/06_decorator)
|
2020-05-02 12:40:51 +03:00
|
|
|
|
+ [x] [代理模式(Proxy)](./structure/07_proxy)
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-05-11 13:05:20 +03:00
|
|
|
|
## 弹性模式 Resiliency Patterns
|
|
|
|
|
|
2020-06-05 07:21:52 +03:00
|
|
|
|
+ [x] [熔断模式(circuit breaker)](./resiliency/01_circuit_breaker)
|
|
|
|
|
+ [x] [限流模式(rate limiting)](./resiliency/02_rate_limiting)
|
|
|
|
|
+ [ ] [WIP][重试模式(retrier)](./resiliency/04_retrier)
|
2020-06-05 12:36:56 +03:00
|
|
|
|
+ [x] [最后期限模式(deadline)](./resiliency/03_deadline)
|
2020-05-11 13:05:20 +03:00
|
|
|
|
|
2020-05-11 13:07:31 +03:00
|
|
|
|
## 更多模式(同步/并发/并行) Go More Patterns(Concurrency/Parallelism/Sync)
|
2020-04-28 17:16:13 +03:00
|
|
|
|
|
2020-05-02 13:25:12 +03:00
|
|
|
|
+ [x] [发布订阅模式(Pub-Sub)](./gomore/01_messages)
|
|
|
|
|
+ [x] [时差模式(Time Profile)](./gomore/02_profiles)
|
|
|
|
|
+ [x] [上下文模式(Context)](./gomore/03_context)
|
2020-05-07 10:06:54 +03:00
|
|
|
|
+ [x] [扇入模式(Fan-In)](./gomore/04_fan_in)
|
2020-05-07 12:57:22 +03:00
|
|
|
|
+ [x] [扇出模式(Fan-Out)](./gomore/05_fan_out)
|
2020-05-04 19:02:06 +03:00
|
|
|
|
+ [ ] [WIP][信号量模式(Semaphore)](./gomore/08_semaphore)
|
|
|
|
|
+ [ ] [WIP][并行模式(Parallelism)](./gomore/09_parallelism)
|
|
|
|
|
+ [ ] [WIP][生成器模式(Generators)](./gomore/10_generators)
|
|
|
|
|
+ [ ] [WIP][屏障模式(N-Barrier)](./gomore/11_n_barrier)
|
|
|
|
|
+ [ ] [WIP][有限并行模式(Bounded Parallelism)](./gomore/12_bounded_parallelism)
|
2020-05-06 12:36:31 +03:00
|
|
|
|
+ [ ] [WIP][批处理模式(batcher)](./gomore/13_batcher)
|
2020-05-11 13:07:31 +03:00
|
|
|
|
|
2020-05-11 13:05:20 +03:00
|
|
|
|
|
2020-05-02 13:43:23 +03:00
|
|
|
|
|
2020-04-27 18:36:22 +03:00
|
|
|
|
## 参考资料(Design patters Articles)
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-05-11 09:28:53 +03:00
|
|
|
|
| Patterns | Instructions | Status |
|
2020-05-05 09:44:57 +03:00
|
|
|
|
|:-------:|:----------- |:------:|
|
2020-05-11 09:28:53 +03:00
|
|
|
|
| [go-patterns](https://github.com/crazybber/go-patterns)|搜集整理到各种模式文章和代码|p|
|
|
|
|
|
| [菜鸟设计模式](https://www.runoob.com/design-pattern/design-pattern-tutorial.html)|以Java为主的设计模式介绍|p|
|
|
|
|
|
| [design_pattern](http://c.biancheng.net/design_pattern)|编程之家的设计模式|p|
|
|
|
|
|
| [golang-design-pattern](https://github.com/senghoo/golang-design-pattern)|go语言的设计模式理论|p|
|
2020-05-11 13:00:55 +03:00
|
|
|
|
| [go-resiliency](https://github.com/eapache/go-resiliency)|go的一些弹性模式 |m|
|
|
|
|
|
| [Behavioral](https://github.com/AlexanderGrom/go-patterns/tree/master/Behavioral)|设计模式中的行为模式(俄语版)|m|
|
2020-05-11 09:28:53 +03:00
|
|
|
|
| [go-patterns](https://github.com/sevenelevenlee/go-patterns)|一些设计模式 |p|
|
2020-05-11 13:00:55 +03:00
|
|
|
|
| [go_design_pattern](https://github.com/monochromegane/go_design_pattern)|日语版设计模式|p|
|
|
|
|
|
| [microsoft-patterns](https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn600223(v=pandp.10))|微软设计模式系列文章(24)|p/w|
|
2020-04-21 17:50:21 +03:00
|
|
|
|
|
2020-05-05 09:44:57 +03:00
|
|
|
|
## 更多 More
|
2020-04-23 04:26:32 +03:00
|
|
|
|
|
2022-09-25 11:04:44 +03:00
|
|
|
|
需要重新温习下Go基础?看这里:
|
2020-04-23 04:26:32 +03:00
|
|
|
|
|
2020-05-03 20:10:21 +03:00
|
|
|
|
[go-fucking-exercises](https://github.com/crazybber/go-fucking-exercise)
|