『iOS 设计模式』笔记
关于笔记
BlueLibrary项目中涉及的模式
MVC模式
在 BlueLibrary 项目中
- Mode 为 Album 类
- View 包括 HorizontalScroller 类 和 Album 类
- Controller 为 ViewController 类
Album 类
- Album 类从 NSObject 类,里面定义了 Album 的属性(五个)
AlbumView 类
- AlbumView 类为 View,直接继承自 UIView
- 根据 MVC 模式的设计原则,AlbumView 不能与 Mode 直接通讯,所以它的代码中没有设计到任何 Album 类
ViewController 类
- ViewController 类用来协调 Album 类 与 多个 View 类之间的工作,这里涉及到的 View 类有 – TableView, HorizontalScroller
Singleton 模式
LibraryAPI 类
- 单例模式的关键在于 – 这个类只有一个实例,且这个实例有且只有一个全局访问点
- 在 LibraryAPI 这个类中,
[[LibraryAPI sharedInstance] someMethed]
就是全局访问点 -
注意 sharedInstance 方法
+ (LibraryAPI *)sharedInstance { // 1 static LibraryAPI *_sharedInstance = nil; // 2 static dispatch_once_t oncePredicate; // 3 dispatch_once(&oncePredicate ,^{ _sharedInstance = [[LibraryAPI alloc] init]; }); return _sharedInstance; }
- 第一行注册一个 _sharedInstance 实例
static
确保变量储存在全局区(静态区)中dispatch_once
确保在多线程环境中,alloc
以及init
只执行一次
Facade 模式
- 门面模式的关键在于 – 只要暴露一个 API 给用户,用户不需要关注 API 后面的代码实现,这样可以简化 API 的调用
PersistencyManager 类
- 负责 Album 类的实例持久化
HTTPClient 类
- HTTPClient 类负责处理远程连接
LibraryAPI 类
- LibraryAPI 的单例用来统一 PersistencyManager 类 和 HTTPClient 类,隐藏了这两类的复杂性
Decorator 模式
- Decorator 分为 Category 和 Delegation
Category
- Category 的作用可以参考链接
Album + TableRepresentation 类
- 为现有的 Album 类增加
-(NSDictionary *)tr_tableRepresentation
方法
Delegation
- 在 ViewController 中需要用到 TableView 类,那么它必须实现 TableView 中 required 的 method
- TableView 中的信息不够,需要 ViewController 求助于它的委托,通过发送消息给委托来获取信息
Adapter 模式
HorizontalScroller 类
- 在 HorizontalScroller 类中,定义
@property (weak) id<HorizontalScrollerDelegate> delegate;
,使它与 Album 类在 ViewController 中耦合 - Delegation 就是一种 Adapter 模式
- 关于 Adapter 模式的介绍 – 原文连接
Observer 模式
- 在观察者模式中,程序可以通过 Notification 和 Key-Value Observing 来实现
Notification 模式
- 发送 Notification 的称为 poster
- 关注 Notification 的为 Observer
-
在 BlueLibrary 项目中,使用 Notification 模式的步骤:
-
观察者注册消息通知 – 在LibraryAPI 类中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadImage:) name:@"BLDownloadImageNotification" object:nil];
-
发送消息通知 – 在 AlbumView 中:
NSDictionary *notificationUserInfo = @{@"imageView":coverImage, @"coverUrl":albumCover}; [[NSNotificationCenter defaultCenter] postNotificationName:@"BLDownloadImageNotification" object:self userInfo:notificationUserInfo];
-
观察者处理消息 – 在 LibraryAPI 类中:
- (void)downloadImage:(NSNotification *)notification
-
观察者注销 – 在 LibraryAPI 类中:
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
-
-
Notification 模式具体介绍 – 点击这里
Key-Value Observing 模式
AlbumView 类
-
增加观察者:
[coverImage addObserver:self forKeyPath:@"image" options:0 context:nil]; // converImage.image -> forKeyPath:@"image"
-
完成的时候,需要注销观察者:
- (void)dealloc { [coverImage removeObserver:self forKeyPath:@"image"]; }
-
每隔观察者实现变化通知:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"image"]) { [indicator stopAnimating]; } }
-
KVO 模式具体介绍 – 点击这里
Memento 模式
NSUserDefaults
NSUserDefaults
用来储存轻量化的数据- 可以支持
NSNumber
,NSString
,NSDate
,NSArray
,NSDictionary
和BOOL
类型 NSUserDefaults
将数据储存在一个 plist 文件中,在沙盒的Library/Preferences/
下
在 BlueLibrary 项目中的应用
- 在 ViewController 中实现当前页面的保存 –
-(void)saveCurrentState
- 再实现载入 method –
-(void)loadPreviousState
- 在 viewDidLoad 中添加一个 Observer,当 app 进入后台时,执行一个 selector 保存当前 Album(注意移除 Observer)
Archiving 模式
介绍
- Mode 类可以用归档来储存,不需要在每次启动的时候重新初始化
- 可以将 object 归档为一个 NSData 类实例
- 再将 NSData 类实例写入到一个文件中