天天看點

Objective-C中設計模式總結

       本篇文章将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

rotatingFooterViewwillAnimateRotationToInterfaceOrientation:duration:willRotateToInterfaceOrientation:duration:

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.你想讓系統支援一個封裝了很多資料變化的業務。這個業務可以被子產品化為一個指令對象。
  1. NSInvocation, NSUndoManager, and the target-action
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.