GOF 23種設計模式

1. 創建型模式

1.1 單例模式 Singleton Pattern

最簡單的設計模式之一, 屬於創建型模式. 它提供了一種創建對象的最佳方式. 這個模式涉及到一個單一的類, 該類負責創建自己的對象, 同時確保只有單個對象被創建. 這個類提供了一種訪問其唯一的對象的方式, 可以直接訪問, 不需要實例化該類的對象.

Ensure a class has only one instance, and provide a global point of access to it.

1.2 工廠方法模式 Factory Method Pattern

定義一個用於創建對象的接口, 讓子類決定實例化哪一個類. 工廠方法是一個類的實例化延遲到子類.

Define an interface for creating an object, but let subclass decide whick class to instantiate. Factory Method lets a class defer instantiation to subclass.

1.3 抽象工廠模式 Abstract Factory Pattern

爲創建一組相關或者相互依賴的對象提供一個接口, 而且無需指定他們的具體類.

Provide an interfacefor creating families of related or dependent objects without specifying their concrete classes.

1.4 創建者模式 Builder Pattern

將一個複雜對象的構建與它的表示分離, 使得同樣的構建過程可以創建不同的表示.

也叫建造者模式, 生成器模式.

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

1.5 原型模式 Prototype Pattern

用原型示例指定創建對象的種類, 並且通過拷貝這些原型創建新的對象.

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

2. 行爲型模式

2.1 迭代器模式 Iterator Pattern

提供一種方法訪問一個容器對象中各個元素, 而不暴露該對象的內部細節.

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

2.2 命令模式 Command Pattern

一種數據驅動的設計模式. 請求以命令的形式包裹在對象中, 並傳給調用對象. 調用對象尋找可以處理該命令的對象, 並把該命令傳給相應的對象, 該對象執行命令.

Encapsulate a request as an object, thereby letting you parameterize clients with different request, queue or log request, and support undoable operations.

2.3 解釋器模式 Interpreter Pattern

提供了評估語言的語法或表達式的方式。這種模式實現了一個表達式接口,該接口解釋一個特定的上下文。它被用在SQL解析、符號處理引擎等。

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

2.4 責任鏈模式 Chain of Responsibility Pattern

使多個對象都有機會處理請求,從而避免了請求的發送者和接受者之間的耦合關係,將這些對象連成一條鏈,並沿着這條鏈傳遞該請求,直到有對象處理爲止。

Avoid coupling the sender of a request to its receiver by giving more than one object a change to handle the request. Chain the receiving object and pass the request along the chain until an object handles it.

2.5 觀察者模式 Observer Pattern

定義對象間一種一對多的依賴關係, 使得每當一個對象改變狀態, 所有依賴於它的對象都會得到通知並被自動更新.

Define a one-to-many dependency between objects so that when an object change state, all its dependents are notified and updated automatically.

2.6 中介者模式 Mediator Pattern

用一箇中介對象封裝一系列對象(同事)的交互, 中介者使各對象不需要顯示的相互作用, 從而使其耦合鬆散, 而且中介者可以獨立地改變他們之間的交互.

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicity, and it lets you vary their interaction independently.

2.7 備忘錄模式 Memento Pattern

在不破壞封裝性的前體下, 捕獲一個對象的內部狀態, 並在該對象之外保存這個狀態. 這樣以後就可將對象恢復到原先保存的狀態.

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

2.8 狀態模式 State Pattern

允許一個對象內在狀態發生改變時改變行爲, 使得這個對象看起來像改變了類型. 狀態模式的核心是封裝, 狀態的變更引起行爲的變動, 從外部看來就好像該對象對應的類發生改變一樣.

Allow an object to alert its behavior when its internal state changes. The object will appear to change its class.

2.9 策略模式 Strategy Pattern

定義一組算法, 將每個算法都封裝起來, 並且他們之間可以相互切換. 類似於"條條大路通羅馬".

Define a family of algorithms, encapsulate each one, and make them interchangeable.

2.10 模板方式模式 Template Method Pattern

定義一個操作彙總的算法的骨架, 然後將一些步驟延遲到子類中. 使得子類可以不改變一個算法的結構即可重新定義該算法的某些特定步驟.

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

2.11 訪問者模式 Visitor Pattern

封裝一些作用於某些數據結構中的各元素的操作, 它可以在不改變數據結構的前提下定義作用於這些元素的新的操作.

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

3. 結構型模式

3.1 適配器模式 Adapter Pattern

將一個類的接口變換成客戶端所期待的另一種接口, 從而使原本因接口不匹配而無法在一起工作的幾個類能夠在一起工作.

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interface.

3.2 組合模式 Composite Pattern

將對象組合成樹形結構以表示"部分-整體"的層次結構, 使得用戶對單個對象和組合對象的使用具有一致性.

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual object and compositions of objects uniformly.

3.3 代理模式 Proxy Pattern

爲其他對象提供一種代理以控制對這個對象的訪問.

Provide a surrogate or placeholder for another object to control access to it.

3.4 橋樑模式 Bridge Pattern

將抽象和實現解耦, 使得兩者可以獨立的變化.

Decouple an abstraction from its implementation so that two can vary independently.

3.5 裝飾模式 Decorator Pattern

動態地給一個對象添加一些額外的職責, 就增加功能來說, 它相比生成子類更加靈活.

Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexiable alternative to subclass for extending functionality.

3.6 門面模式 Facade Pattern

要求一個子系統的外部與其他內部的通信必須通過一個統一的對象進行. 門面模式提供高層次的接口, 使得子系統更易於使用.

Provide a unified interface to a set of interfaces in a subsystem. Facede defines a higher-level interface that makes the subsystem easier to use.

3.7 享元模式 Flyweight Pattern

使用共享對象可以有效地支持大量的細力度的對象.

Use sharing to support large numbers of fine-grained objects efficiently.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章