本篇文章將Gang of Four中所列舉的設計模式在Objective-C中的使用從名稱,定義,何時使用,UML圖以及在Cocoa Touch中的應用五個維度做出了總結。具體的列表請看下圖。因爲本人水平有限,所以有錯誤或者不正確的地方歡迎提出意見和建議。
名稱
|
定義
|
何時使用
|
UML圖
|
Cocoa Touch中的使用
|
Prototype(原型) |
Specify the kinds of objects to create using a prototypical instance,and create new objects by copying this prototype. |
1.創建的對象獨立於它是什麼和它是如何被創建的;
2.要實例化的類在運行時刻指定;
3.我們不想有一個和產品一個層次的工廠;
4.不同類的實例間的差別只是一些組合的狀態,通過複製來創建更方便一些;
5.類並不容易被創建時,比如組合中的對象又各自包含對象時。
|
NSCopying代理中的
copyWithZone方法。
|
|
Factory Method(工廠方法) |
Define an interface for creating an object, but letsubclasses decide which class to instantiate. Factory Method lets a class defer instantiation tosubclasses. |
1.需要創建的對象的類在編譯時無法預料;
2.一個類想讓其子類在運行時絕對創建什麼;
3.一個類擁有一些幫助類作爲它的子類,並且你想要具體返回哪一個的局部知識。
|
Factory methods can be seen almost everywhere in the Cocoa Touch framework. Such that some “convenience” methods that
return aninstance of the class.
|
|
Abstract Factory(抽象工廠) |
Provides an interface for creating families of related or dependent objectswithout specifying their concrete classes. |
|||
Builder(創建者) |
Separates the construction of a complex object from its representationso that the same construction process can create different representations.
|
1.你想要創建一個複雜的包含好幾個步驟的對象。創建它的算法應該獨立於每個部分是如何創建的。
2.你需要一個用不同步驟創建一個對象的創建步驟。
|
||
Singleton(單例) |
Ensures a class has only one instance, and provide a global point ofaccess to it. |
1.某個類必須有且只有一個實例,並且只能通過一個被熟知的方法訪問。
2.這個僅有的實例僅僅可以通過子類化來進行拓展,而且拓展不會破壞client代碼。
|
UIApplication, UIAccelerometer, and NSFileManager. |
|
Adapter(適配器) |
Converts the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
|
1.已存在的類的接口不符合你的要求。
2.你想要一個可以複用的類和一個也許沒有合適的接口的類協作。
3.你需要適配一個類的很多子類,但是又不可能讓它們每個類都生成一個適配器子類。所以你可以使用一個對象適配器(delegate)來適配它們父類的方法。
|
|
delegate, block |
Bridge(橋接) |
Decouples an abstraction from its implementation so that the two canvary independently. |
1.你不想將一個抽象和它的實現永遠綁定在一起。
2.抽象和它的實現都會被子類拓展。
3.改變一個抽象的實現並不會影響client code
4.如果爲了使一個實現更好而需要一個額外的子類,那麼說明需要把實現和抽象分離了。
5.你想將一個實現在不同對象的抽象中進行分享。
|
||
Facade(外觀) |
Provides a unified interface to a set of interfaces in a system. Façadedefines a higher-level interface that makes the subsystem easier to use. |
1.你的子系統變得特別複雜。爲了實現一個功能需要涉及好的的類,你就可以使用一個外觀來提供一個操作這些類的簡單的接口。
2.你可以使用外觀來對你的子系統進行分層。每個子系統都只有一個外觀進行訪問。你可以讓他們只簡單地通過外觀來進行通信。
|
||
Mediator(中介者) |
Defines an object that encapsulates how a set of objects interacts.Mediator promotes loose coupling by keeping objects from referring to each other explicitly, andit lets you vary their interaction independently. |
1.一組對象互相獨立並且由於它們之間複雜的交互難以理解它們的關係。
2.由於一個對象和其他的很多對象相互指向和交互使其難以重複利用。
3.一個邏輯或者行爲被在很多類中分發則需要不通過子類化的方式進行配置。
|
||
Observer (觀察者) |
Defines a one-to-many dependency between objects so that whenone object changes state, all its dependents are notified and updated automatically. |
1.有兩種抽象並且彼此依賴。將它們封裝在不同的對象中允許你獨立地改變和重用它們。
2.一個對象的改變也要求改變別的對象,並且被改變的對象的數量是不固定的。
3.一個對象需要在不知道別的對象是什麼的情況下通知其他對象。
|
Notification, KVO | |
Composite(複合) |
Compose objects into tree structures to represent part-wholehierarchies. Composite lets clients treat individual objects and compositions of objectsuniformly. |
1.你想用抽象樹來呈現一些對象。
2.你想要client以相同的方式對待一組對象和獨立的對象。
|
UIViews | |
Iterator(遍歷者) |
Provide a way to access to the elements of an aggregate object sequentially withoutexposing its underlying representation. |
1.你需要在不暴露一個複合對象的內部表現的情況下訪問一個複合對象中的內容。
2.你需要以不同的方式穿越複合對象。
3.你需要提供一種統一的方式來遍歷不同類型的複合對象。
|
NSEnumerator | |
Visitor(訪問者) |
The Visitor pattern represents an operation to be performed on the elements of an objectstructure. Visitor lets you define a new operation without changing the classes of the elements onwhich it operates. |
1.一個複雜的對象結構包含很多的其他的有着不同接口的對象,你想要根據他們實際的類型做不同的操作。
2.你想要對一個複合結構中的對象做無關的操作,並且這些操作不會污染這些類。你可以把有關的操作放在一個訪問這類中然後在需要時使用這些操作。
3.你經常需要向一個結構穩定的類中增加新的複雜操作。
|
||
Decorator(裝飾者) |
Attaches additional responsibilities to an object dynamically.Decorators provide a flexible alternative to subclassing for extending functionality. |
1.你想要動態地和顯示地並且不影響其他對象地爲一些對象增加一些操作。
2.你想要給一個不可能拓展的類增加新的行爲。當一個類的定義是隱藏的或者不允許被子類化。
3.拓展一個類的行爲是可選擇的時候。
|
||
Chain of Responsibility (職責鏈) |
To avoid coupling the sender of a request to its receiverby giving more than one object a chance to handle the request. It chains the receiving objectsand passes the request along the chain until an object handles it. |
1.有一個以上的對象可能對一個請求進行處理,但是具體是哪一個只有在運行時才知道。
2.你想要對一組的對象發起一個請求但是並不指定究竟是哪一個來處理這個請求。
|
||
Template Method(模板方法) |
Define the skeleton of an algorithm in an operation,deferring some steps to subclasses. Template Method lets subclasses redefine certain steps ofan algorithm without changing the algorithm's structure. |
1.你需要一次性實現一個算法中不變的部分並且把那些特殊的行爲留到子類中去實現。
2.子類中通用的行爲可以被放入一個公用類中避免代碼冗餘。已存在代碼中的差異需要被放進新的操作中。你使用模板方法代替這些新的代碼時調用每個新的操作。
3.子類的拓展必須是可控的。你可以在某個點上定義一個”hook”操作。子類可以在hook上進行拓展操作。
|
UIView中的drawRect:方法
rotatingHeaderView |
|
Strategy(策略) |
Define a family of algorithms, encapsulate each one, and makethem interchangeable. Strategy lets the algorithm vary independently from clients that use it. |
1.一個類在它的操作中使用不同的條件來定義不同的行爲。你可以將相關的條件分支放進他們自己的策略類中。
2.你在一個算法中需要不同的變體。
3.你需要避免在用戶面前暴露算法的複雜性和算法相關的數據。
|
||
Command(命令) |
Encapsulate a request as an object, thereby letting you parameterizeclients with different requests, queue or log requests, and support undoable operations. |
1.你想讓你的程序支持undo/redo
2.你想將一個動作參數化爲一個對象並通過不同的命令來執行操作和替換回調。
3.你想要指定,隊列和執行一個請求在不同的時間點。
4.你想要記錄變化所以他們可以再系統故障時被重新恢復。
5.你想讓系統支持一個封裝了很多數據變化的業務。這個業務可以被模塊化爲一個命令對象。
|
|
|
Flyweight(享元) |
Uses sharing to support large numbers of fine-grained objectsefficiently. |
1.你的應用使用很多對象。
2.在內存中加載對象會影響內存性能。
3.很多對象的唯一狀態可以被外部化或者輕量級化。
4.一些相關的被分享的對象可以替換原來的一些對象,在對象的唯一狀態被去掉後。
5.你的應用不依賴對象的ID因爲被分享的對象不可以有唯一標示符。
|
||
Proxy(代理) |
Provides a surrogate or placeholder for another object to control accessto it. |
1.你需要一個遙遠的代理提供一個本地的表現性來代表一個在不同位置上的對象或者互聯網上的對象。
2.你需要一個虛擬代理來創建重量級的對象,根據要求。
3.你需要一個保護代理來基於不同的訪問權限控制對於原始對象的訪問請求。
4.你需要一個智能引用代理來對訪問真正對象的引用進行計數。它同樣可以用來鎖住原始對象來讓其他對象無法修改它。
|
NSProxy | |
Memento(備忘錄) |
Without violating encapsulation, capture and externalize an object’sinternal state so that the object can be restored to this state later. |
1.你需要保存一個對象的狀態爲snapshot或者它的一部分,將來可以將其進行恢復。
2.你需要隱藏這個如果獲得狀態就會暴露其實現的接口。
|
The Cocoa Touch framework has adopted the Memento pattern with archiving, propertylist serialization, and core data. |