首页 / 设计模式 / iOS常用设计模式——观察者设计模式
iOS常用设计模式——观察者设计模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS常用设计模式——观察者设计模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3998字,纯文字阅读大概需要6分钟。
内容图文
观察者设计模式详解
基本概念
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己.而在IOS开发中我们可能会接触到的经典观察者模式的实现方式,有这么几种:NSNotificationCenter、KVO、Delegate等
NSNotificationCenter的使用
添加监听
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
sample:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:GET_UP object:nil];
说明:
【NSNotificationCenter defaultCenter】消息中心只有一个,通过该方法获取该类的单例
addObserver:添加监听者
selector:表示监听到到对象后的处理函数
name:表示注册所关心的消息名称
接收消息
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString )aName object:(id)anObject userInfo:(NSDictionary )aUserInfo;
sample:
[[NSNotificationCenter defaultCenter] postNotificationName:GET_UP object:[NSNumber numberWithInteger:self.time]];
分析:
GET_UP:推送消息的名称
object:发送消息的对象
userInfo:发送消息携带的信息
删除监视
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
KVO的使用
基本概念
KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。该方法与NSNotification有很大的不同,它并不需要一个NotificationCenter来为所有观察者提供变化通知,相反的是,当有变化发生时,该通知直接发送给观察者,NSObject为我们实现了此方法。
利用此方法我们可以观察对象,是一种一对多的关系。
注册观察者
首先必须发送一个
addObserver:selfforKeyPath:@”happyNum” options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld
context:nil]消息至观察对象,用以传送观察对象和需要观察的属性的关键路径,参数分析:
- (void)addObserver:(NSObject )observer forKeyPath:(NSString )keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
observer:表示观察者是谁,一般为self
keyPath: 表示观察哪个对象
options:NSKeyValueObservingOptionNew表示观察新值,NSKeyValueObservingOptionOld表示观察旧值
context:表示上下文环境
观察者对象发生变化时的回调方法
-
(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {NSLog(@”%@”,change);
}
参数分析:
change:表示发生改变时返回的值
keyPath:表示观察对象
contenst:表示上下文
remove观察者身份
发送一条制定观察方对象和路径来移除观察者,该函数一般放在dealloc:
[self.childremoveObserver:selfforKeyPath:@”happyNum”context:nil];
代码实现
child.h#import <Foundation/Foundation.h>@interfaceChild : NSObject@property(nonatomic,assign)NSInteger happyNum;
@end
child.m#import "Child.h"@implementationChild
-(id)init
{
self=[super init];
if(self)
{
self.happyNum=100;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(decrease:) userInfo:nil repeats:YES];
}
returnself;
}
-(void)decrease:(NSTimer*)timer
{
// NSLog(@"the happyNum is %ld",self.happyNum);
_happyNum--;
// [self setValue:[NSNumber numberWithInteger:_happyNum] forKey:@"happyNum"];
}
@end
nurse.h#import <Foundation/Foundation.h>#import "Child.h"@interfaceNurse : NSObject@property(nonatomic,retain)Child *child;
-(id)initWithChild:(Child*)child;
@end
nurse.m:
#import "Nurse.h"@implementationNurse
-(id)initWithChild:(Child*)child
{
self=[super init];
if(self)
{
self.child = child;
[self.child addObserver:self forKeyPath:@"happyNum"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
}
returnself;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"%@",change);
}
-(void)dealloc
{
[self.child removeObserver:self forKeyPath:@"happyNum" context:nil];
[_child release];
[super dealloc];
}
@end
main.m:
#import <Foundation/Foundation.h>#import "Child.h"#import "Nurse.h"int main(int argc, constchar * argv[])
{
@autoreleasepool {
Child *childObj=[[[Child alloc] init] autorelease];
Nurse *nurseObj=[[[Nurse alloc] initWithChild:childObj] autorelease];
[[NSRunLoop currentRunLoop] run];
//[nurseObj release];
}
return0;
}
原文:http://blog.csdn.net/richard_rufeng/article/details/44781501
内容总结
以上是互联网集市为您收集整理的iOS常用设计模式——观察者设计模式全部内容,希望文章能够帮你解决iOS常用设计模式——观察者设计模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。