观察者模式
- 观察者模式本质上时一种发布-订阅模型,用以消除具有不同行为的对象之间的耦合,通过这一模式,不同对象可以协同工作,同时它们也可以被复用于其他地方Observer从Subject订阅通知,ConcreteObserver实现重现ObServer并将其重载其update方法。一旦SubJect的实例需要通知Observer任何新的变更,Subject会发送update消息来通知存储在其内部类中所注册的Observer、在ConcreteObserver的update方法的实际实现中,Subject的内部状态可被取得并进行后续处理。其类图如下
观察者UML
- 可以发现观察者模式无非在是定义对象间的一种一对多的依赖关系,并且当一个对象的状态发生改变的时候,所有依赖于它的对象都会得到通知且自动更新。即如果Subject允许其他观察者(实现了观察者接口的对象)对这个Subject的改变进行请阅,当Subject发送了变化,那么Subject会将这个变化发送给所有的观察者,观察者就能对Subject的变化做出更新.
观察者demo实例
- ObserverCenterProtocol 协议类
#import <Foundation/Foundation.h>
@protocol ObserverCenterProtocol <NSObject>
//用户的信息和从某一刊物接受的信息
- (void)accpetUserMessage:(id)userMessage subNumber:(NSString *)subNumber;
@end
#import <Foundation/Foundation.h>
#import "ObserverCenterProtocol.h"
@interface ObserverCenter : NSObject
// 创建书刊订阅号
+ (void)createNumber:(NSString *)observerNumber;
//移除订阅号
+ (void)removeNumebr:(NSString *)observerNumer;
//添加用户
+ (void)addUser:(id<ObserverCenterProtocol>)user withNumebr:(NSString *)observerNumber;
//移除用户
+ (void)removeUser:(id<ObserverCenterProtocol>)user withNumebr:(NSString *)observerNumber;
//发送消息
+ (void)sendMessage:(id)message withNumber:(NSString *)number;
@end
#import "ObserverCenter.h"
static NSMutableDictionary *_observerDict = nil;
@implementation ObserverCenter
+ (void)initialize
{
if (self == [ObserverCenter class]) {
_observerDict = [NSMutableDictionary dictionary];
}
}
//创建书刊订阅号
+ (void)createNumber:(NSString *)observerNumber
{
NSHashTable *hashTable = [self existNumber:observerNumber];
if (hashTable == nil) {
hashTable = [NSHashTable weakObjectsHashTable];
[_observerDict setObject:hashTable forKey:observerNumber];
}
}
//移除订阅号
+ (void)removeNumebr:(NSString *)observerNumer
{
NSHashTable *hashTable = [self existNumber:observerNumer];
if (hashTable) {
[_observerDict removeObjectForKey:observerNumer];
}
}
//添加用户
+ (void)addUser:(id<ObserverCenterProtocol>)user withNumebr:(NSString *)observerNumber
{
NSHashTable *hashTable = [self existNumber:observerNumber];
[hashTable addObject:user];
}
//移除用户
+ (void)removeUser:(id<ObserverCenterProtocol>)user withNumebr:(NSString *)observerNumber
{
NSHashTable *hashTable = [self existNumber:observerNumber];
[hashTable removeObject:user];
}
//发送消息
+ (void)sendMessage:(id)message withNumber:(NSString *)number
{
//获取订阅号
NSHashTable *hashTable = [self existNumber:number];
if (hashTable) {
NSEnumerator *enumerator = [hashTable objectEnumerator];
id <ObserverCenterProtocol> object = nil;
while (object = [enumerator nextObject]) {
NSLog(@"xxxxxx:%@",[enumerator nextObject]);
if ([object respondsToSelector:@selector(accpetUserMessage:subNumber:)]) {
[object accpetUserMessage:message subNumber:number];
}
}
}
}
+ (NSHashTable *)existNumber:(NSString *)number
{
return [_observerDict objectForKey:number];
}
@end
#import "ViewController.h"
#import "ObserverCenter.h"
static NSString *OBSERVER = @"OBSERVER";
@interface ViewController ()<ObserverCenterProtocol>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建订阅号
[ObserverCenter createNumber:OBSERVER];
// 添加订阅的用户到指定的书刊上
[ObserverCenter addUser:self withNumebr:OBSERVER];
// 发送消息
[ObserverCenter sendMessage:@"2.22" withNumber:OBSERVER ];
}
- (void)accpetUserMessage:(id)userMessage subNumber:(NSString *)subNumber
{
NSLog(@"uuserMessagese ====== :%@ subNumber ======:%@",userMessage,subNumber);
}
@end