Go语言设计模式(概览)
A curated collection of idiomatic design & application patterns for Go language。
这是一组用于Go语言的惯用设计和应用模式
Creational Patterns 创造者模式
- Abstract Factory:provides an interface for creating families of releated objects 抽象工厂: 提供了一个接口来创建被释放对象的家族
- Builder: Builds a complex object using objects 建造者模式: 构建一个对象基于简单的对象
- Factory Method: Defers instantiation of an object to a specialized function for creating instances 方法工厂化:将对象的实例化根据特定的用于创建实例的专用函数来创建各自的实例
- Object Pool: Instantiates and maintains a group of objects instances of the same type 对象池模式:实例化和维护一组相同类型的对象实例,(吃了吐再吃了吐)
- Singleton: Restricts instancation of a type to one object 单例模式:将类型的实例化限制为一个对象
Structural Patterns 结构化模式
- Bridge: Decouples an interface from its implementation so that the two can vary independently 桥接模式:将接口与其实现解耦,从而使两者能够独立地变化 (基于原有的接口进行封装,类似于再包装,保持之前的也可以)
- Composite: Encapsulates and provides access to a number of different objects 组合模式:封装并提供对许多不同对象的访问
- Decorator:Adds behavior to an object, statically or dynamically 装饰器模式:向对象添加静态或动态的行为
- Facade:Uses one type as an API to a number of others 外观模式(类似于多态):将一种类型用作许多其他类型的API
- Proxy:Provides a surrogate for an object to control it's actions 代理模式:提供对象的代理来控制其操作(根据具体的情况使用不同的操作)
Behavioral Patterns 行为模式
- Observer:Provide a callback for notification of events/changes to data 观察者模式:为事件/数据更改通知提供回调
- Strategy: Enables an algorithm's behavior to be selected at runtime 策略模式:允许在运行时选择算法的行为
Synchronization Patterns 同步模式
- Semaphore: Allows controlling access to a common resource 信号量模式:允许控制对公共资源的访问
-
Read-Write Lock:Allows parallel read access, but only exclusive access on write operations to a resource 读写锁模式:允许并行读访问,但仅对资源的写操作进行独占访问
- Monitor:Combination of mutex and condition variable patterns 监视器模式:互斥锁和条件变量模式的组合
- Condition Variable: Provides a mechanism for threads to temporarily give up access in order to wait for some condition 条件变量模式:提供线程临时放弃访问以等待某些条件的机制
- Lock/Mutex:Enforces mutual exclusion limit on a resource to gain exclusive access 锁模式:对资源施加互斥限制以获得独占访问
Concurrency Patterns:并发模式
- Bounded Parallelism:Completes large number of independent tasks with resource limits 有界并发模式:完成大量有资源限制的独立任务
- Broadcast:Transfers a message to all recipients simultaneously 广播模式:同时将消息传递给所有收件人
- Coroutines:Subroutines that allow suspending and resuming execution at certain locations 协程模式:允许在某些位置挂起和恢复执行的子例程
- Generators:Yields a sequence of values one at a time 生成器模式:每次生成一个值序列
- Parallelism:Completes large number of independent tasks 并发模式:完成大量独立任务
- Producer Consumer: Separates tasks from task executions 生成消费者模式:将任务与任务执行分离
Messaging Patterns 消息传递模式
- Fan-In :Funnels tasks to a work sink (e.g. server) 扇入模式:将任务分配给工作接收器(如服务器)
- Fan-Out: Distributes tasks among workers (e.g. producer) 扇出模式:在工人之间分配任务(例如,生产者)
- Futures & Promises:Acts as a place-holder of a result that is initially unknown for synchronization purposes 期货和承诺模式:充当结果的位置占位符,该结果最初在同步时是未知的
- Publish/Subscribe: Passes information to a collection of recipients who subscribed to a topic 发布订阅模式:将信息传递给订阅主题的收件人集合
- Push & Pull: Distributes messages to multiple workers, arranged in a pipeline 消息管道模式:将消息分发给多个工作者,并安排在管道中
Stability Patterns 稳定模式
- Circuit-Breaker: Stops the flow of the requests when requests are likely to fail 熔断模式:当请求可能失败时,停止请求流
-
Deadline :Allows clients to stop waiting for a response once the probability of response becomes low (e.g. after waiting 10 seconds for a page refresh) 限期模式:允许客户端在响应概率变低时停止等待响应(例如,在等待页面刷新10秒后)