Objective-C中設計模式總結,objectivec設計模式

來源:互聯網
上載者:User

Objective-C中設計模式總結,objectivec設計模式

       本篇文章將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(Factory 方法)

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, andNSFileManager.


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 code4.如果為了使一個實現更好而需要一個額外的子類,那麼說明需要把實現和抽象分離了。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/redo2.你想將一個動作參數化為一個對象並通過不同的命令來執行操作和替換回調。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.



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.