iOS 观察者模式

  • 观察者模式
    用户在订阅中心注册订阅号,通知中心往订阅号发消息,用户接收消息,执行通知中心协议方法。
    系统通知中心使用后必须释放,自定义通知中心可以实现优化。

  • 应用,适用场景
    订阅中心

自定义通知中心

//
//  SubscriptionServiceCenter.h
//  LearnObserver
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SubscriptionServiceCenterProtocol.h"

@interface SubscriptionServiceCenter : NSObject

#pragma mark - 维护订阅信息
///创建订阅号
+ (void)createSubscriptionNumber:(NSString *)subscriptionNumber;
///移除订阅号
+ (void)removeSubscriptionNumber:(NSString *)subscriptionNumber;

#pragma mark - 维护客户信息
///添加客户到具体的订阅号当中
+ (void)addCustomer:(id <SubscriptionServiceCenterProtocol>)customer withSubscriptionNumber:(NSString *)subscriptionNumber;
///从具体的订阅号当中移除客户
+ (void)removeCustomer:(id <SubscriptionServiceCenterProtocol>)customer withSubscriptionNumber:(NSString *)subscriptionNumber;

#pragma mark - 发送消息
///发送消息到具体的订阅号当中
+ (void)sendMessage:(id)message toSubscriptionNumber:(NSString *)subscriptionNumber;

@end
//
//  SubscriptionServiceCenter.m
//  LearnObserver
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "SubscriptionServiceCenter.h"

static NSMutableDictionary *_subscriptionDictionary = nil;

@implementation SubscriptionServiceCenter

+ (void)initialize {
    if (self == [SubscriptionServiceCenter class]) {
        _subscriptionDictionary = [NSMutableDictionary dictionary];
    }
}

+ (void)createSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = [self existSubscriptionNumber:subscriptionNumber];
    if (hashTable == nil) {
        hashTable = [NSHashTable weakObjectsHashTable];
        [_subscriptionDictionary setObject:hashTable forKey:subscriptionNumber];
    }
}

+ (void)removeSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = [self existSubscriptionNumber:subscriptionNumber];
    if (hashTable) {
        [_subscriptionDictionary removeObjectForKey:subscriptionNumber];
    }
}

+ (void)addCustomer:(id <SubscriptionServiceCenterProtocol>)customer withSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(customer);
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = [self existSubscriptionNumber:subscriptionNumber];
    [hashTable addObject:customer];
}

+ (void)removeCustomer:(id <SubscriptionServiceCenterProtocol>)customer withSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = [self existSubscriptionNumber:subscriptionNumber];
    [hashTable removeObject:customer];
}

+ (void)sendMessage:(id)message toSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = [self existSubscriptionNumber:subscriptionNumber];
    if (hashTable) {
        NSEnumerator *enumerator = [hashTable objectEnumerator];
        id <SubscriptionServiceCenterProtocol> object = nil;
        while (object = [enumerator nextObject]) {
            if ([object respondsToSelector:@selector(subscriptionMessage:subscriptionNumber:)]) {
                [object subscriptionMessage:message subscriptionNumber:subscriptionNumber];
            }
        }
    }
}

#pragma mark - 私有方法
+ (NSHashTable *)existSubscriptionNumber:(NSString *)subscriptionNumber {
    return [_subscriptionDictionary objectForKey:subscriptionNumber];
}

@end

自定义通知中心协议

//
//  SubscriptionServiceCenterProtocol.h
//  LearnObserver
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol SubscriptionServiceCenterProtocol <NSObject>

@required
- (void)subscriptionMessage:(id)message subscriptionNumber:(NSString *)subscriptionNumber;

@end

使用自定义通知中心

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNotificationCenter];
}

- (void)customNotificationCenter {
    ///创建订阅号
    [SubscriptionServiceCenter createSubscriptionNumber:SCIENCE];
    ///添加订阅的用户到指定的刊物
    [SubscriptionServiceCenter addCustomer:self withSubscriptionNumber:SCIENCE];
    ///发行机构发送消息
    [SubscriptionServiceCenter sendMessage:@"V1.0" toSubscriptionNumber:SCIENCE];
}

#pragma mark - 自定义通知中心方法
- (void)subscriptionMessage:(id)message subscriptionNumber:(NSString *)subscriptionNumber {
    NSLog(@"%@  %@", message, subscriptionNumber);
}

使用系统通知中心

- (void)viewDidLoad {
    [super viewDidLoad];
    [self systemNotificationCenter];
}

- (void)systemNotificationCenter {
    ///添加客户到指定的订阅号中
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCenterEvent:) name:SCIENCE object:nil];
    ///发送信息到指定的订阅号当中
    [[NSNotificationCenter defaultCenter] postNotificationName:SCIENCE object:@"V1.0"];
    
    ///创建订阅中心
    self.model = [Model new];
    ///客户添加了订阅中心的"name"服务
    [self.model addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    ///订阅中心发送消息(通过修改属性值)
    self.model.name = @"V1.0";
}

#pragma mark - 通知中心方法
- (void)notificationCenterEvent:(id)sender {
    NSLog(@"%@", sender);
}

#pragma mark - 释放资源
- (void)dealloc {
    ///移除通知中心
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:SCIENCE];
}

系统通知中心KVO实现

- (void)viewDidLoad {
    [super viewDidLoad];
    [self notificationCenterByKVO];
}

- (void)notificationCenterByKVO {
    ///创建订阅中心
    self.model = [Model new];
    ///客户添加了订阅中心的"name"服务
    [self.model addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    ///订阅中心发送消息(通过修改属性值)
    self.model.name = @"V1.0";
}

#pragma mark - KVO方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"%@", change);
}

#pragma mark - 释放资源
- (void)dealloc {
    ///移除KVO
    [self.model removeObserver:self forKeyPath:@"name"];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 观察者模式本质上时一种发布-订阅模型,用以消除具有不同行为的对象之间的耦合,通过这一模式,不同对象可以协同工作,同...
    PlatonsDream阅读 1,323评论 0 0
  • 什么是观察者模式?当A对B的变化感兴趣,需要监听B的状态变化,就注册为B的观察者,当B发生变化时通知A,告知B发生...
    _Lily阅读 1,620评论 2 4
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,860评论 25 709
  • 一.什么是观察者模式? 简单的说就是一个对象拥有多个特征,当某一个特征发生变化时,另外一个对象做出相应的处理和操作...
    LYSNote阅读 4,238评论 0 5
  • 本文整理了帮你的站点创建出一个优秀的“关于”页面的9个想法: 1、让访问者能轻易的联系到你 在某些情况下,访问者可...
    三达不留点gpj阅读 736评论 0 5