PHP观察者通知机制,观察者模式-通知详解
觀察者模式也叫發(fā)布/訂閱模式,是軟件設(shè)計模式中的一種。在這種模式中,一個目標(biāo)物件管理所有相依于它的觀察者物件,并且在它本身的狀態(tài)改變時主動發(fā)出通知。這通常透過呼叫各觀察者所提供的方法來實(shí)現(xiàn)。此種模式通常被用來實(shí)現(xiàn)事件處理系統(tǒng)。
觀察者模式的類圖如下:
屏幕快照 2018-08-22 上午10.34.11.png
可以看出它有四個角色,具體如下:
抽象主題(Subject):抽象主題是一個協(xié)議,它是一個觀察者的集合容器,定義添加觀察者(attach)方法,移除觀察者(detach)方法和所有觀察者發(fā)送通知的方法(notifyObserve)。
抽象觀察者(Observe):抽象觀察者也是一個協(xié)議,它有一個更新(update)方法。
具體觀察者(ConcreteObserve):觀察協(xié)議具體實(shí)現(xiàn)。
具體主題(ConcerteSubject):主題協(xié)議的具體實(shí)現(xiàn)。
從圖中可以看出,引入Observe和Subject這兩個協(xié)議后,觀察者模式可以完美的將觀察者和被觀察的對象分離開,不僅提高了系統(tǒng)的可復(fù)用行,還降低了耦合度。
在Cocoa Touch框架中,觀察者模式的具體應(yīng)用有兩個 - 通知(notification)機(jī)制和KVO(Key-ValueObserving)機(jī)制。
通知機(jī)制
通知機(jī)制與委托機(jī)制不同的是,前者是“一對多”的對象之間的通信,后者是“一對一”的對象之間的通信。
在通知機(jī)制中對某個通知感興趣的所有對象可以成為接收者首先,對這些對象需要向通知中心(NSNotificationCenter)發(fā)出addObserve:選擇器:名稱:對象消息進(jìn)行注冊,在投送對象投送通知給通知中新世,通知中心就會把通知廣播給注冊過的接收者。所有的接收者都不知道是誰投送的,更不關(guān)心細(xì)節(jié)。投送對象與接收者是一對多的關(guān)系。接收者如果對通知不再關(guān)注,會給通知中心發(fā)出removeObserver:名稱:對象:消息解除注冊,以后不再接收通知。
屏幕快照 2018-08-22 上午11.31.38.png
注冊通知:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(resignNotification:) name:@"login" object:nil];
/** 換種方式 */
__weak typeof(self) weakSelf = self;
[[NSNotificationCenter defaultCenter]addObserverForName:@"drain" object:nil queue:[NSOperationQueue new] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"你瞅啥");
NSLog(@"接收到通知:%@",[NSThread currentThread]);
}];
}
-(void)resignNotification:(NSNotification *)notification{
NSLog(@"%s",__func__);
NSLog(@"接收到通知:%@",[NSThread currentThread]);
}
發(fā)送同步通知:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[NSNotificationCenter defaultCenter]postNotificationName:@"login" object:nil userInfo:@{}];
}
發(fā)送異步通知:
/**
* NSPostingStyle有三種:
NSPostNow:與postNotificationName相同(就是在哪個線程里面就是獲取當(dāng)前線程的通知隊(duì)列并且默認(rèn)采用NSPostNow發(fā)送時機(jī))
NSPostASAP:不立即發(fā)出通知,而是在runloop匹配時調(diào)用,即:runloop處理事件源時
NSPostWhenIdle:runloop閑置的時候post,即:runloop進(jìn)入睡眠時
*/
/*
NSNotificationCoalescing消息合并的方式
NSNotificationNoCoalescing = 0, //不合并
NSNotificationCoalescingOnName = 1,//按名稱合并
NSNotificationCoalescingOnSender = 2,//按發(fā)送者合并
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//每個線程都默認(rèn)又一個通知隊(duì)列,可以直接獲取,也可以alloc
NSNotificationQueue *notificationQueue = [NSNotificationQueue defaultQueue];
NSNotification *notification = [NSNotification notificationWithName:@"drain" object:nil];
NSNotification * notificationtwo = [NSNotification notificationWithName:@"drain" object:nil];
NSLog(@"發(fā)送通知before:%@",[NSThread currentThread]);
[notificationQueue enqueueNotification:notification postingStyle:NSPostNow coalesceMask:NSNotificationNoCoalescing forModes:nil];
[notificationQueue enqueueNotification:notificationtwo postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
[notificationQueue enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
NSLog(@"發(fā)送通知After:%@",[NSThread currentThread]);
}
移除通知:
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"login" object:nil];
//就是
輸出結(jié)果:
2018-08-22 15:20:53.382286+0800 AngelClient[4074:168069] -[ViewController resignNotification:]
2018-08-22 15:20:53.382421+0800 AngelClient[4074:168069] 接收到通知:{number = 1, name = main}
2018-08-22 15:20:53.382513+0800 AngelClient[4074:168069] 發(fā)送通知before:{number = 1, name = main}
2018-08-22 15:20:53.382889+0800 AngelClient[4074:168112] 你瞅啥
2018-08-22 15:20:53.383015+0800 AngelClient[4074:168112] 接收到通知:{number = 3, name = (null)}
2018-08-22 15:20:53.383172+0800 AngelClient[4074:168069] 發(fā)送通知After:{number = 1, name = main}
2018-08-22 15:20:53.383594+0800 AngelClient[4074:168112] 你瞅啥
2018-08-22 15:20:53.383741+0800 AngelClient[4074:168112] 接收到通知:{number = 3, name = (null)}
NSNotificationCenter是單例模式,創(chuàng)建獲得共享實(shí)例的方法defaultCenter。其中名稱是通知的名字,對象是投送通知時傳遞過來的對象(不一定是self對象,如果接收者不需要,可以將其設(shè)為nil ),USERINFO是投送通知時定義的字典對象,可以用于參數(shù)的傳遞,進(jìn)行的是同步發(fā)送.NSNotificationQueue是將發(fā)送的通知加到當(dāng)前線程的隊(duì)列中,進(jìn)行異步發(fā)送。
下一篇:觀察者模式-KVO詳解
總結(jié)
以上是生活随笔為你收集整理的PHP观察者通知机制,观察者模式-通知详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决兼容性的库
- 下一篇: php网站xml链接,xml图像超链接的