Design Patterns

🧷:go-design-patterns

Creational Pattern

Factory Method 工廠模式

Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

提供一個創建物件的介面在一個超類別中,但是允許子類別改變創建的物件類型。

Abstract Factory 抽象工廠

Lets you produce families of related objects without specifying their concrete classes.

讓你可以生產一系列相關的物件,而不需要指定具體的類別。

Builder 建造者模式

Lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

讓你逐步構建複雜的物件。這種模式允許你使用相同的構建代碼來產生不同類型和表示的物件。

Prototype 原型模式

Lets you copy existing objects without making your code dependent on their classes.

讓你複製一個已有物件來產生新的物件,而不需要通過實例化。這種模式允許你通過拷貝已有物件的狀態來創建新的物件。

Singleton 單例模式

Lets you ensure that a class has only one instance, while providing a global access point to this instance.

確保一個類別僅有一個實例,同時提供一個全局訪問點來訪問這個實例。

Structural Pattern

Adapter 適配器模式

Allows objects with incompatible interfaces to collaborate.

允許具有不相容接口的物件之間進行協作。
  ---
title: Go Adapter Example
---
classDiagram

    class XMLData {

    }

    class JSONData {

    }

    class XMLService {
        +ParseXML(xmlData []byte)
    }

    class XMLToJSONAdapter {
        -xmlService *XMLService
        +ConvertToJSON(xml []byte)
    }

    class Client {
        -xmlService *XMLService
        -xmlToJSONAdapter *XMLToJSONAdapter
        +ProcessXMLData(data []byte)
        +ProcessJSONData(data []byte)
    }

    XMLService *.. XMLToJSONAdapter
    XMLService ..> XMLData
    XMLToJSONAdapter ..> JSONData
    Client ..* XMLToJSONAdapter
    Client ..* XMLService

Bridge 橋接模式

Lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.

將一個大類別或一組相關的類別分為兩個獨立的層次結構:抽象和實現。這兩個層次可以獨立地進行開發。

Composite 組合模式

Lets you compose objects into tree structures and then work with these structures as if they were individual objects.

允許你將物件組成樹狀結構,然後像處理單個物件一樣處理這些結構。

Decorator 裝飾者模式

Lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

允許你通過將物件放入特殊的包裝器物件中來附加新的行為。

Facade 外觀模式

Provides a simplified interface to a library, a framework, or any other complex set of classes.

為一個庫、一個框架或任何其他複雜的類別集合提供一個簡化的接口。

Flyweight 享元模式

Lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object.

通過在多個物件之間共享相同的狀態部分,而不是在每個物件中保留所有數據,來更有效地利用可用的內存空間。

Proxy 代理模式

Lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

允許你為另一個物件提供一個代理或佔位符。代理控制對原始物件的訪問,允許你在請求到達原始物件之前或之後執行一些操作。

Behavior Pattern

Observer 觀察者模式

Lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

允許你定義一個訂閱機制,通知多個觀察該物件的對象有關其發生的任何事件。

Strategy 策略模式

Lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

允許你定義一族算法,將每個算法放入單獨的類別中,並使它們的物件可以互換使用。

Chain of Responsibility 責任鏈模式

Lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

允許你將請求沿著一個處理器鏈傳遞。每個處理器收到請求後,可以決定是處理請求還是將其傳遞給鏈中的下一個處理器。

Command 命令模式

Turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.

將請求轉換為獨立的物件,該物件包含有關請求的所有信息。這種轉換允許你將請求作為方法參數傳遞、延遲或排隊請求的執行,以及支持可撤消的操作。

Iterator 迭代器模式

Lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

允許你遍歷集合的元素,而無需揭示其底層表示(列表、堆疊、樹等)。

Mediator 中介者模式

Lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.

允許你減少物件之間的混亂相依性。該模式限制了物件之間的直接通信,並強制它們只能通過一個中介者物件進行協作。

Memento 備忘錄模式

Lets you save and restore the previous state of an object without revealing the details of its implementation.

允許你在不暴露其實現細節的情況下保存和恢復物件的先前狀態。

State 狀態模式

Lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.

當物件的內部狀態改變時,允許物件改變其行為。這看起來就像物件改變了其類別。

Template Method 模板方法模式

Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.

在超類別中定義算法的骨架,但允許子類別覆蓋算法的特定步驟,而不改變其結構。

Visitor 訪問者模式

Lets you separate algorithms from the objects on which they operate.

允許你將算法與其作用的物件分離開來。

Interpreter 解譯器模式

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

給定一種語言,定義其文法的表示方式,並提供一個解譯器來使用這個表示方式來解釋該語言中的句子。