A curated collection of idiomatic design & application patterns for Go language.
| Pattern | Description | Status |
|---|---|---|
| Abstract Factory抽象工厂 | Provides an interface for creating families of releated objects | ✘ |
| Builder 生成器 | Builds a complex object using simple 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 instantiation of a type to one object | ✔ |
| Pattern | Description | Status |
|---|---|---|
| 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 | ✘ |
| Flyweight 享元模式 | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✘ |
| Proxy 代理模式 | Provides a surrogate for an object to control it's actions | ✔ |
| Pattern | Description | Status |
|---|---|---|
| Chain of Responsibility 责任链模式 | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✘ |
| Command 命令模式 | Bundles a command and arguments to call later | ✘ |
| Mediator 终结者模式 | Connects objects and acts as a proxy | ✘ |
| Memento 备忘录模式 | Generate an opaque token that can be used to go back to a previous state | ✘ |
| Observer 观察者模式 | Provide a callback for notification of events/changes to data | ✔ |
| Registry 注册模式 | Keep track of all subclasses of a given class | ✘ |
| State 状态模式 | Encapsulates varying behavior for the same object based on its internal state | ✘ |
| Strategy 策略模式 | Enables an algorithm's behavior to be selected at runtime | ✔ |
| Template 模版模式 | Defines a skeleton class which defers some methods to subclasses | ✘ |
| Visitor 访问者模式 | Separates an algorithm from an object on which it operates | ✘ |
| Pattern | Description | Status |
|---|---|---|
| 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 | ✘ |
| Monitor 监听者模式 | Combination of mutex and condition variable patterns | ✘ |
| Read-Write Lock 读写锁模式 | Allows parallel read access, but only exclusive access on write operations to a resource | ✘ |
| Semaphore 信号模式 | Allows controlling access to a common resource | ✔ |
| Pattern | Description | Status |
|---|---|---|
| N-Barrier | Prevents a process from proceeding until all N processes reach to the barrier | ✘ |
| 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 | ✔ |
| Reactor 反应器模式 | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ |
| Parallelism 并行模式 | Completes large number of independent tasks | ✔ |
| Producer Consumer 生产者消费者模式 | Separates tasks from task executions | ✘ |
| Pattern | Description | Status |
|---|---|---|
| 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 | ✘ |
| Pattern | Description | Status |
|---|---|---|
| Bulkheads | Enforces a principle of failure containment (i.e. prevents cascading failures) | ✘ |
| 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) | ✘ |
| Fail-Fast | Checks the availability of required resources at the start of a request and fails if the requirements are not satisfied | ✘ |
| Handshaking | Asks a component if it can take any more load, if it can't, the request is declined | ✘ |
| Steady-State | For every service that accumulates a resource, some other service must recycle that resource | ✘ |
| Pattern | Description | Status |
|---|---|---|
| Timing Functions | Wraps a function and logs the execution | ✔ |
| Pattern | Description | Status |
|---|---|---|
| Functional Options | Allows creating clean APIs with sane defaults and idiomatic overrides | ✔ |
| Pattern | Description | Status |
|---|---|---|
| Cascading Failures | A failure in a system of interconnected parts in which the failure of a part causes a domino effect | ✘ |
