一堆Go语言惯例和应用模式,翻译自go patterns,翻译目的主要是为了自己学习go语言。
模式 | 描述 | 状态 |
---|---|---|
抽象工厂Abstract Factory | 一个用于创建相关对象族的接口 | ✘ |
构建器Builder | 利用简单对象构建一个复杂对象 | ✔ |
工厂方法Factory Method | 将对象创建工作推迟委派给一个指定的函数 | ✔ |
对象池Object Pool | 实例化并维护一组相同类型的对象实例 | ✔ |
单例Singleton | 限制只能实例化一个对象 | ✔ |
模式 | 描述 | 状态 |
---|---|---|
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 | ✘ |
模式 | 描述 | 状态 |
---|---|---|
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 | ✘ |
模式 | 描述 | 状态 |
---|---|---|
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 | ✔ |
模式 | 描述 | 状态 |
---|---|---|
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 | ✘ |
模式 | 描述 | 状态 |
---|---|---|
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 | ✘ |
模式 | 描述 | 状态 |
---|---|---|
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 | ✘ |
模式 | 描述 | 状态 |
---|---|---|
Timing Functions | Wraps a function and logs the execution | ✔ |
模式 | 描述 | 状态 |
---|---|---|
Functional Options | Allows creating clean APIs with sane defaults and idiomatic overrides | ✔ |
模式 | 描述 | 状态 |
---|---|---|
Cascading Failures | A failure in a system of interconnected parts in which the failure of a part causes a domino effect | ✘ |