一、观察者模式(通知机制,KVO机制)
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。 简而言之,就是A和B,A对B的变化感兴趣,就注册A为观察者,当B发生变化时通知A,告知B发生了变化。这个也叫做经典观察者模式。
ViewController.m
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property(nonatomic,strong)Person *child;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.child = [[Person alloc]init];
NSLog(@"%d",self.child.age);
[self.child addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"aaa"];
[self.child addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"bb"];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
btn.backgroundColor = [UIColor orangeColor];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//监听回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
NSLog(@"监听回调");
NSLog(@"%@,%@,%@",object,change,context);
NSLog(@"新的:%@",[change objectForKey:@"new"]);
}
-(void)click
{
[self.child changeAgeValue:100 :@"张三"];
}
-(void)dealloc
{
[self removeObserver:self.child forKeyPath:@"age"];
}
Person.m
-(instancetype)init
{
self = [super init];
if (self)
{
self.age = 10;
self.name=@"张三";
}
return self;
}
-(void)changeAgeValue:(int)number :(NSString *)newName
{
self.age = number;
self.name=newName;
}
//监听回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
NSLog(@"%@,%@,%@",object,change,context);
}
Person.h
@interface Person : NSObject
@property(nonatomic,assign)int age;
@property(nonatomic,strong)NSString *name;
-(void)changeAgeValue:(int)number :(NSString *)newName;
@end
二、代理模式
代理模式是一种消息传递方式,一个完整的代理模式包括:委托对象、代理对象和协议。
协议:
用来指定代理双方可以做什么,必须做什么。
委托对象:
根据协议指定代理对象需要完成的事,即调用协议中的方法。
代理对象:
根据协议实现委托方需要完成的事,即实现协议中的方法。
实例:
ViewController.m
#import "ViewController.h"
#import "TimerControl.h"
@interface ViewController ()<UpdateAlertDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TimerControl *timer = [[TimerControl alloc] init];
timer.delegate = self; //设置代理实例
NSLog(@"调用TimerController");
[timer startTheTimer];//启动定时器,定时5触发 //=====1
}
- (void)didReceiveMemoryWarning
{
NSLog(@"销毁");
[super didReceiveMemoryWarning]; //=====5
// Dispose of any resources that can be recreated.
}
//"被代理对象"实现协议声明的方法,由"代理对象"调用
- (void)updateAlert:(NSString *)text
{
// UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
//
// alert.alertViewStyle=UIAlertViewStyleDefault;
// [alert show];
NSLog(@"回到ViewController,%@",text); //=====4
}
TimerControl.h
#import "TimerControl.h"
@implementation TimerControl
- (void) startTheTimer
{
NSLog(@"设置计时器");
[NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerProc) userInfo:nil repeats:NO];//=====2
}
- (void) timerProc
{
//当代理响应updateAlert的方法时,让其执行。 不安全
if (_delegate && [_delegate respondsToSelector:@selector(updateAlert:)]) {
NSLog(@"TimerControl处理");
[self.delegate updateAlert:@"你好呀"];//代理更新UI //=====3
}
}
@end
TimerControl.m
#import <Foundation/Foundation.h>
//协议定义
@protocol UpdateAlertDelegate <NSObject>
- (void)updateAlert:(NSString *)text;
@end
//UpdateAlertDelegate协议名
@interface TimerControl : NSObject
//遵循协议的一个代理变量定义
@property (nonatomic, weak) id<UpdateAlertDelegate> delegate;
- (void) startTheTimer;
@end
此时TimerControl就是委托对象、ViewController就是代理对象、UpdateAlertDelegate就是协议。