程式設計師必看--23種設計模式及解釋(中英文對照)

gaopengtttt發表於2017-08-04

轉自:http://blog.csdn.net/yaolingrui/article/details/7338263

設計模式是每個程式設計師的必修課,這裡將23種模式整理在一起,想學習這方面知識的朋友們有福了,呵呵。


Singleton Pattern(單例模式):Ensure a class has only one instance, and provide a global point of access to it.(確保某一個類只有一個例項,而且自行例項化並向整個系統提供這個例項。)


Factory Pattern(工廠方法模式):Define an interface for creating an object,

but let subclass decide which class to instantiate.Factory Method lets a class defer instantiation to subclass.(定義一個用於建立物件的介面,讓子類決定例項化哪一個類。工廠方法是一個類的例項化延遲到其子類。)


Abstract Factory Pattern(抽象工廠模式):Provide an interface for creating families of related or dependent objects without specifying their concrete classes.(為建立一組相關或相互依賴的物件提供一個介面,而且無需指定它們的具體類。)


Template Method Pattern(模板方法模式):Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.

Template Method lets subclass redefine certain steps of an algorithm without changing the algorithm's structure. 

(定義一個操作中的框架,而將一些步驟延遲到子類中。使得子類可以不改變一個演算法的結構即可以重定義該演算法的某些特定步驟。)


Builder Pattern(建造者模式): Separate the construction of a complex object form its representation so that the

 same construction process can create different representations.(將一個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示。)


Proxy pattern(代理模式):Provide a surrogate (代理) or placeholder for another object to control access to it.(為其他物件提供一種代理以控制對這個物件的訪問。)


Prototype Pattern(原型模式):Specify the kinds of objects to create using a prototypical instance,

 and create new objects by copying this prototype.(用原型例項指定建立物件的種類,並且透過複製這些原型建立新的物件。)


Mediator Pattern(中介者模式)Define an object that encapsulates how a set of objects interact.

Mediator promotes loose couping by keeping objects from referring to each other explicitly, 

and it lets you vary their interaction independently.

(用一箇中介物件封裝一系列的物件互動,中介者使各物件不需要顯示的相互作用,從而使其耦合鬆散,而且可以獨立的改變它們之間的互動。)


命令模式

Encapsulate a request as an object,thereby letting you parameterize clients with different requests,queue or log requests, and support undoable operations.(將一個請求封裝成一個物件,從而讓你使用不同的請求把客戶端引數化,對請求排隊或者記錄請求日誌,可以提供命令的撤銷和恢復功能。)

責任鏈模式

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.Chain the receiving objects and pass the request along the chain until an object handles it.(使多個物件有機會處理請求,從而避免了請求的傳送者和接收者之間的耦合關係 。將這些物件連成一個鏈,並沿著這條鏈傳遞請求,知道有物件處理它為止。)

裝飾模式(Decorator Pattern)

Attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alternative to subclassing for extending functionality.(動態地給一個物件新增一些額外的職責。就增加功能來說,裝飾模式相比生成子類更為靈活。)


策略模式(Strategy Pattern)

Define a family of algorithms, encapsulate each one, and make them interchangeable.(定義一組演算法,將每個演算法都封裝起來,並且使他們之間可以互換。)


介面卡模式(Adapter Pattern)

Convert the inface of a class into another interface clients expect.Adapter lets classes work together that couldn't otherwise because of incompatible interface.(將一個類的介面變換成客戶端所期待的另一種介面,從而使原本因介面不匹配而無法在一起工作的兩個類能夠在一起工作。)
“系統的資料和行為都正確,單介面不符時,我們應該考慮使用介面卡,目的是是控制範圍之外的一個原有物件與某個介面匹配。介面卡模式主要用於希望複用一些現存的類,但是介面又與複用環境不一致的情況。”(《大話設計模式》)


迭代器模式(Iterator Pattern)

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.(它提供一種方法訪問一個容器物件中各個元素,而又不需要暴露該物件的內部細節。)


組合模式(Composite Pattern)

Compose objects into tree structure to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.(將物件組合成樹形結構以表示“部分-整體”的層次結構,使得使用者對單個物件和組合物件的使用具有一致性。)


觀察者模式(Observer Pattern)

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.(定義物件間一種一對多的依賴關係,使得每當一個物件改變狀態,則所有依賴於它的物件都會得到通知並被自動更新。)


門面模式(Facade Pattern)Provide a unified interface to a set of interface in a subsystem.Facede defines a higher-level interface that makes the subsystem easier to use.(要求一個子系統的外部與其內部的通訊必須透過一個統一的物件進行。門面模式提供了一個高層次的介面,使得子系統更容易使用。)


備忘錄模式(Memento Pattern)

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.(在不破壞封裝的前提下,捕獲一個物件的內部狀態,並在該物件之外儲存這個狀態,這樣以後就可將該物件恢復到原來儲存的狀態。)


訪問者模式(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.(封裝一些作用於某種中的各種元素,它可以在不改變資料結構的前提下定義作用於這些元素的新的操作。)


狀態模式

Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.(當一個物件在狀態改變時允許其改變行為,這個物件看起來像改變了其類。)


直譯器模式(Interpreter Pattern)

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences int the language.(給定一門語言,定義它的文法的一種表示,並定義一個直譯器,該直譯器使用該表示來解釋語言中的句子。)/p>


享元模式(Flyweight Pattern)

Use sharing to support large numbers of fine-grained objects efficiently.(使用共享物件可有效地支援大量的細粒度物件。)


橋樑模式(Bridge Pattern)

Decouple an abstraction from its implementation so that the two can vary independently.(將抽象和實現解耦,使得兩者可以獨立的變化。)


注:本文中的內容參考的是秦小波的《設計模式之禪》。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2143112/,如需轉載,請註明出處,否則將追究法律責任。

相關文章