了解設(shè)計(jì)模式的意圖,是在代碼重構(gòu)中浮現(xiàn)并識(shí)別設(shè)計(jì)模式的關(guān)鍵吱涉。
本文將四巨頭在《設(shè)計(jì)模式》一書(shū)的23種設(shè)計(jì)模式的意圖放在一個(gè)頁(yè)面里功咒,方便查閱孙技。四巨頭把這23種設(shè)計(jì)模式按照創(chuàng)建倍谜、結(jié)構(gòu)忍饰、行為分為三類卡辰。
創(chuàng)建(1. 抽象工廠蛆橡、2. 建造者钠右、3. 工廠方法赋元、4. 原型、5. 單例)
-
抽象工廠
Intent意圖:Provide an interface for creatingfamilies of related or dependent objects without specifying their concreteclasses. 提供一個(gè)接口飒房,來(lái)創(chuàng)建一些彼此相關(guān)或依賴的對(duì)象族搁凸,而無(wú)須指定其具體的類。
Abstract Factory抽象工廠 -
建造者
Intent意圖:Separate the construction of acomplex object from its representation so that the same construction processcan create different representations. 將一個(gè)復(fù)雜對(duì)象的構(gòu)造與其表現(xiàn)形式進(jìn)行分離狠毯,使得同樣的構(gòu)造過(guò)程能創(chuàng)建出不同的表現(xiàn)形式护糖。
Builder建造者 -
工廠方法
Intent意圖:Define an interface for creatingan object, but let subclasses decide which class to instantiate. Factory Methodlets a class defer instantiation to subclasses. 定義一個(gè)用于創(chuàng)建一個(gè)對(duì)象的接口,但讓子類來(lái)決定具體實(shí)例化哪個(gè)類嚼松。工廠方法模式能讓一個(gè)類將實(shí)例化的工作延遲到各個(gè)子類中來(lái)進(jìn)行嫡良。
Factory Method工廠方法 -
原型
Intent意圖:Specify the kinds of objects tocreate using a prototypical instance, and create new objects by copying thisprototype. 使用一個(gè)原型實(shí)例來(lái)指定所要?jiǎng)?chuàng)建的對(duì)象的類型,并通過(guò)復(fù)制該原型來(lái)創(chuàng)建新的對(duì)象献酗。
Prototype原型 -
單例
Intent意圖:Ensure a class only has oneinstance, and provide a global point of access to it. 確保一個(gè)類僅有一個(gè)實(shí)例寝受,并提供一個(gè)對(duì)其進(jìn)行訪問(wèn)的全局訪問(wèn)點(diǎn)。
Singleton單例
結(jié)構(gòu)(1. 適配器罕偎、2. 橋接很澄、3. 組合、4. 裝飾器锨亏、5. 門(mén)面痴怨、6. 輕量級(jí)忙干、7. 代理)
-
適配器
Intent意圖:Convert the interface of a classinto another interface clients expect. Adapter lets classes work together thatcouldn't otherwise because of incompatible interfaces. 將一個(gè)類的接口轉(zhuǎn)換成其客戶端所期望的另一個(gè)接口器予。適配器模式能讓以前因?yàn)榻涌诓患嫒荻鵁o(wú)法協(xié)同工作的一些類可以在一起工作。
Adapter適配器 -
橋接
Intent意圖:Decouple an abstraction from its implementation so that the two can vary independently. 將一個(gè)抽象與其實(shí)現(xiàn)解耦合捐迫,使得兩者能夠獨(dú)立地發(fā)生改變乾翔。
Bridge橋接 -
組合
Intent意圖:Compose objects into treestructures to represent part-whole hierarchies. Composite lets clients treatindividual objects and compositions of objects uniformly. 將若干對(duì)象組合成樹(shù)的結(jié)構(gòu),來(lái)表示“部分-整體”的層次結(jié)構(gòu)施戴。組合模式讓客戶端能統(tǒng)一地對(duì)待單個(gè)對(duì)象和對(duì)象的復(fù)合體反浓。
Composite組合 -
裝飾器
Intent意圖:Attach additionalresponsibilities to an object dynamically. Decorators provide a flexiblealternative to subclassing for extending functionality. 為一個(gè)對(duì)象動(dòng)態(tài)地附加額外的職責(zé)。除了子類化這個(gè)方案以外赞哗,裝飾器模式為擴(kuò)展代碼功能提供了另一個(gè)靈活的備選方案雷则。
Decorator裝飾器 -
門(mén)面
Intent意圖:Provide a unified interface to aset of interfaces in a subsystem. Facade defines a higher-level interface thatmakes the subsystem easier to use. 為一個(gè)子系統(tǒng)中的一系列接口提供一個(gè)統(tǒng)一的接口。門(mén)面模式定義了一個(gè)更高層次的接口肪笋,而令該子系統(tǒng)使用起來(lái)更加容易月劈。
Facade門(mén)面 -
輕量級(jí)
Intent意圖:Use sharing to support largenumbers of fine-grained objects efficiently. 使用共享來(lái)有效地支持巨大數(shù)量的細(xì)粒度的對(duì)象的使用度迂。
Flyweight輕量級(jí) -
代理
Intent意圖:Provide a surrogate orplaceholder for another object to control access to it. 為另一個(gè)對(duì)象提供一個(gè)代理或占位者,來(lái)控制對(duì)該對(duì)象的訪問(wèn)猜揪。
Proxy代理
行為(1. 責(zé)任鏈惭墓、2. 命令、3. 翻譯器而姐、4. 迭代器腊凶、5. 中介者、6. 紀(jì)念品拴念、7. 觀察者钧萍、8. 狀態(tài)、9. 策略政鼠、10. 模版方法划煮、11. 訪問(wèn)者)
-
責(zé)任鏈
Intent意圖:Avoid coupling the sender of arequest to its receiver by giving more than one object a chance to handle therequest. Chain the receiving objects and pass the request along the chain untilan object handles it. 通過(guò)讓多個(gè)對(duì)象各自有一次機(jī)會(huì)來(lái)處理一個(gè)請(qǐng)求的方式,來(lái)避免將一個(gè)請(qǐng)求的發(fā)送者與其接受者相耦合缔俄。將各個(gè)接收該請(qǐng)求的對(duì)象鏈接起來(lái)弛秋,并將該請(qǐng)求沿著這個(gè)鏈條傳遞下去,直到有一個(gè)對(duì)象處理了該請(qǐng)求為止俐载。
Chain of Responsibility責(zé)任鏈 -
命令
Intent意圖:Encapsulate a request as anobject, thereby letting you parameterize clients with different requests, queueor log requests, and support undoable operations. 以一個(gè)對(duì)象的形式封裝一個(gè)請(qǐng)求蟹略,從而能用不同請(qǐng)求來(lái)對(duì)客戶端進(jìn)行參數(shù)化,將請(qǐng)求進(jìn)行排隊(duì)或記錄日志遏佣,并支持可撤銷(xiāo)的操作挖炬。
Command命令 -
翻譯器
Intent意圖:Given a language, define arepresention for its grammar along with an interpreter that uses therepresentation to interpret sentences in the language. 給定一種語(yǔ)言,針對(duì)其語(yǔ)法定義一個(gè)表示形式状婶,并提供一個(gè)能使用該表示形式的翻譯器意敛,來(lái)翻譯這種語(yǔ)言的語(yǔ)句。
Interpreter翻譯者 -
迭代器
Intent意圖:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 提供一種方法來(lái)順序訪問(wèn)一個(gè)聚合對(duì)象的各個(gè)元素膛虫,而無(wú)須暴露該對(duì)象的底層表示草姻。
Iterator迭代器 -
中介者
Intent意圖:Define an object thatencapsulates how a set of objects interact. Mediator promotes loose coupling bykeeping objects from referring to each other explicitly, and it lets you varytheir interaction independently. 定義了一個(gè)對(duì)象,來(lái)將一系列對(duì)象之間如何進(jìn)行交互進(jìn)行封裝稍刀。中介者模式能通過(guò)讓這些對(duì)象免于顯式地相互進(jìn)行引用而促進(jìn)松耦合撩独,并能夠做到獨(dú)立地去改變這些對(duì)象之間的交互。
Mediator中介者 -
紀(jì)念品
Intent意圖:Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. 在不破壞封裝的前提下账月,將一個(gè)對(duì)象的內(nèi)部狀態(tài)進(jìn)行捕獲并外部化综膀,使得該對(duì)象能夠在以后被恢復(fù)到這個(gè)內(nèi)部狀態(tài)。
Memento紀(jì)念品 -
觀察者
Intent意圖:Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 在若干對(duì)象之間定義一個(gè)一對(duì)多的依賴關(guān)系局齿,使得當(dāng)一個(gè)對(duì)象改變狀態(tài)時(shí)剧劝,其所有的依賴者都被通知并被自動(dòng)更新。
Observer觀察者 -
狀態(tài)
Intent意圖:Allow an object to alter itsbehavior when its internal state changes. The object will appear to change itsclass. 當(dāng)一個(gè)對(duì)象的內(nèi)部狀態(tài)改變時(shí)抓歼,允許該對(duì)象相應(yīng)地改變其行為讥此。這樣該對(duì)象看起來(lái)好像改變了其所屬的類示绊。
State狀態(tài) -
策略
Intent意圖:Define a family of algorithms,encapsulate each one, and make them interchangeable. Strategy lets thealgorithm vary independently from clients that use it. 定義一個(gè)算法族,對(duì)每一個(gè)算法進(jìn)行封裝暂论,并令其能夠?qū)崿F(xiàn)互換面褐。策略模式能讓算法獨(dú)立于那些使用它的客戶端而發(fā)生變化。
Strategy策略 -
模版方法
Intent意圖:Define the skeleton of analgorithm in an operation, deferring some steps to subclasses. Template Methodlets subclasses redefine certain steps of an algorithm without changing thealgorithm's structure. 定義一個(gè)操作中的算法的骨架取胎,并將其中一些步驟延遲到若干子類中去執(zhí)行展哭。模板方法模式能讓這些子類重新定義一個(gè)算法的某些步驟,而無(wú)須改變?cè)撍惴ǖ慕Y(jié)構(gòu)闻蛀。
Template Method模板方法 -
訪問(wèn)者
Intent意圖:Represent an operation to beperformed on the elements of an object structure. Visitor lets you define a newoperation without changing the classes of the elements on which it operates. 用來(lái)表示即將要施加到一個(gè)對(duì)象結(jié)構(gòu)的各個(gè)元素之上的一個(gè)操作匪傍。訪問(wèn)者模式能用來(lái)定義一個(gè)新的操作,而無(wú)須改變其所操作的上述各個(gè)元素所屬的類觉痛。
Visitor訪問(wèn)者