重點(diǎn)在通知模式流程圖,大致了解幾種通知模式的用途的區(qū)別髓废。都是理論的東西巷懈,還是希望實(shí)際寫代碼時聯(lián)系理論,仔細(xì)考慮選擇慌洪。
課件下載:
https://github.com/gewill/GeekBand-iOS-Demo/tree/master/Design%20Patterns
6. 委托模式
- 復(fù)雜的模型,scrollView,tableView,collectionView
- 單?一個類無法表現(xiàn)復(fù)雜的設(shè)計
- 設(shè)計拆分
- 方便重?
- delegate 獨(dú)立對象
- 清晰定義功能,變化行為/自定義界?面
- 松散耦合,容易擴(kuò)展
以 Master-Detail Application 模板詳細(xì)介紹了委托模式顶燕。孔老師喜歡直接看類的定義冈爹。
![UITableView delegation](http://i.imgur.com/ijupTTB.png)
![Jump to Definition](http://i.imgur.com/QPtgCFl.png)
![UITableViewDataSource](http://i.imgur.com/eUzOn9O.png)
7. 觀察者和消息通知
![MVC](http://i.imgur.com/i8HTac7.png)
![Observer pattern](http://cdn.cocimg.com/cms/uploads/allimg/130506/4196_130506095606_1.png)
- 定義對象間一種?對多的依賴關(guān)系,使得每當(dāng)一個對象改變狀態(tài),則所有依賴于他的對象都會得到通知并被自動更新涌攻。
- Subject被觀察者:定義被觀察者必須實(shí)現(xiàn)的職責(zé),它必須能夠動態(tài)的增加、取消 觀察者频伤。它一般是抽象類或者是實(shí)現(xiàn)類,僅僅完成作為被觀察者必須實(shí)現(xiàn)的職責(zé)
:管理觀察者并通知觀察者 - Observer觀察者:觀察者接收到消息后,即進(jìn)行update(更新方法)操作,對接收到的信息進(jìn)行處理恳谎。
- 具體的被觀察者:定義被觀察者自己的業(yè)務(wù)邏輯,同時定義對哪些事件進(jìn)行通知。
- 具體的觀察者:每個觀察者在接收到消息后的處理反應(yīng)是不同的,各個觀察者有自己的處理邏輯憋肖。
通知
![Notification pattern](http://i.imgur.com/18qyxxi.png)
應(yīng)用場景:
- 窗口變化通知
- 系統(tǒng)鍵盤的出現(xiàn)和消失/位置?小變化
- UITextField 字符變化通知(可以用來限制輸入長度)
- MPMoviePlayerController 播放器的?為變化(開始結(jié)束等事件)
- 自定義Class使用
代碼實(shí)現(xiàn)參看李久寧的文章:iOS 設(shè)計模式之四:觀察者模式
Key-Value-Coding and Key-Value-Observing
可在 Xcode 中 Open Quickly(??O)因痛,查看NSKeyValueCoding.h
協(xié)議的內(nèi)容婚苹。
典型的例子 NSOperation and NSOperationQueue
/* NSOperation.h
Copyright (c) 2006-2014, Apple Inc. All rights reserved.
*/
@interface NSOperation : NSObject {
@private
id _private;
int32_t _private1;
#if __LP64__
int32_t _private1b;
#endif
}
- (void)start;
- (void)main;
@property (readonly, getter=isCancelled) BOOL cancelled;
- (void)cancel;
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isConcurrent) BOOL concurrent; // To be deprecated; use and override 'asynchronous' below
@property (readonly, getter=isAsynchronous) BOOL asynchronous NS_AVAILABLE(10_8, 7_0);
@property (readonly, getter=isReady) BOOL ready;
延伸閱讀:
-
Apple Key-Value Coding Programming Guide
This document describes the NSKeyValueCoding informal protocol, which defines a mechanism allowing applications to access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables.
Dot Syntax and Key-Value Coding: Objective-C’s dot syntax and key-value coding are orthogonal technologies. You can use key-value coding whether or not you use the dot syntax, and you can use the dot syntax whether or not you use KVC. Both, though, make use of a “dot syntax.” In the case of key-value coding, the syntax is used to delimit elements in a key path. Remember that when you access a property using the dot syntax, you invoke the receiver’s standard accessor methods.
-
我們會常常提及“接收者”和“發(fā)送者”。它們在消息傳遞中的意思可以通過以下的例子解釋:一個 table view 是發(fā)送者婚肆,它的 delegate 就是接收者租副。Core Data managed object context 是它所發(fā)出的 notification 的發(fā)送者,獲取 notification 的就是接收者较性。一個滑塊 (slider) 是 action 消息的發(fā)送者用僧,而實(shí)現(xiàn)這個 action (方法)的是它的接收者。任何修改一個支持 KVO 的對象的對象是發(fā)送者赞咙,這個 KVO 對象的觀察者就是接收者责循。明白精髓了嗎?
基于不同消息傳遞機(jī)制的特點(diǎn)的流程圖
communication-patterns-flow-chart
9. 歸檔和解檔
NSCoding
是一個簡單的協(xié)議攀操,有兩個方法: -initWithCoder: 和 encodeWithCoder:院仿。遵循NSCoding協(xié)議的類可以被序列化和反序列化,這樣可以歸檔到磁盤上或分發(fā)到網(wǎng)絡(luò)上速和。
@interface Book : NSObject <NSCoding>
@property NSString *title;
@property NSString *author;
@property NSUInteger pageCount;
@property NSSet *categories;
@property (getter = isAvailable) BOOL available;
@end
@implementation Book
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}
self.title = [decoder decodeObjectForKey:@"title"];
self.author = [decoder decodeObjectForKey:@"author"];
self.pageCount = [decoder decodeIntegerForKey:@"pageCount"];
self.categories = [decoder decodeObjectForKey:@"categories"];
self.available = [decoder decodeBoolForKey:@"available"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.title forKey:@"title"];
[encoder encodeObject:self.author forKey:@"author"];
[encoder encodeInteger:self.pageCount forKey:@"pageCount"];
[encoder encodeObject:self.categories forKey:@"categories"];
[encoder encodeBool:[self isAvailable] forKey:@"available"];
}
@end
NSKeyedArchiver 和 NSKeyedUnarchiver
提供了很方便的API把對象讀取/寫入磁盤歹垫。一個基于NSCoding的table view controller可以通過file manager設(shè)置它的屬性集合。
[NSKeyedArchiver archiveRootObject:books toFile:@"/path/to/archive"];
[NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/archive"];
NSUserDefaults
每個應(yīng)用程序都有自己的user preferences颠放,它可以存儲和檢索遵循NSCoding協(xié)議的對象或者是C類型數(shù)據(jù)排惨。
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:books];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"books"];
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"books"];
NSArray *books = [NSKeyedUnarchiver unarchiveObjectWithData:data];
延伸閱讀:
10. 復(fù)制模式
- 創(chuàng)建一個對象的新副本
- 復(fù)制一個復(fù)雜對象時,保護(hù)一個一樣的對象,還是包含原來對象的副本
- 用戶界面上的復(fù)制/粘貼 有些對象封裝了獨(dú)一無?的資源,復(fù)制沒有意義
- 淺復(fù)制和深復(fù)制。顧名思義,淺復(fù)制,并不拷?對象本?,僅僅是拷貝指向?qū)ο蟮闹羔?深復(fù)制是直接拷貝整個對象內(nèi)存到另?塊內(nèi)存中
- initWithDictionary:copyItems
就是個典型例子碰凶,可深可淺暮芭。
參看 MicroCai 的文章:iOS 集合的深復(fù)制與淺復(fù)制