观察者

2019-03-06  本文已影响0人  XZhongWen

iOS 设计模式 - 观察者

原理图

Observer.png

说明

代码实现

订阅者对象都遵守的协议

//
//  SubscriptionProtocol.h
//  Observer
//
//  Created by mye on 2019/3/4.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@protocol SubscriptionProtocol <NSObject>
@optional
/**
 *  接收到的订阅信息
 *
 *  @param message            订阅信息
 *  @param subscriptionNumber 订阅编号
 */
- (void)subscriptionMessage:(id)message subscriptionNumber:(NSString *)subscriptionNumber;

@end

NS_ASSUME_NONNULL_END

订阅服务中心

//
//  SubscriptionServerCenter.h
//  Observer
//
//  Created by mye on 2019/3/4.
//  Copyright © 2019 mye. All rights reserved.
//

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

NS_ASSUME_NONNULL_BEGIN

@interface SubscriptionServerCenter : NSObject

/**
 *  创建订阅号
 *
 *  @param subscriptionNumber 订阅号码
 */
- (void)createSubscriptionNumber:(NSString *)subscriptionNumber;

/**
 *  移除订阅号(参与到该订阅号码的所有客户不会再收到订阅信息)
 *
 *  @param subscriptionNumber 订阅号码
 */
- (void)removeSubscriptionNumber:(NSString *)subscriptionNumber;

/**
 *  客户订阅指定的订阅号
 *
 *  @param customer           客户对象
 *  @param subscriptionNumber 订阅号码
 */
- (void)addCustomer:(id<SubscriptionProtocol>)customer
withSubscriptionNumber:(NSString *)subscriptionNumber;

/**
 *  将指定客户从指定订阅号上移除掉
 *
 *  @param customer           客户对象
 *  @param subscriptionNumber 订阅号码
 */
- (void)removeCustomer:(id<SubscriptionProtocol>)customer
fromSubscriptionNumber:(NSString *)subscriptionNumber;

/**
 *  通知签订了订阅号码的对象
 *
 *  @param message            信息对象
 *  @param subscriptionNumber 订阅号码
 */
- (void)sendMessage:(id)message
toSubscriptionNumber:(NSString *)subscriptionNumber;

@end

NS_ASSUME_NONNULL_END

//
//  SubscriptionServerCenter.m
//  Observer
//
//  Created by mye on 2019/3/4.
//  Copyright © 2019 mye. All rights reserved.
//

#import "SubscriptionServerCenter.h"

static NSMutableDictionary *_subscriptionNumberDictionary = nil;

@implementation SubscriptionServerCenter

+ (void)initialize {
    _subscriptionNumberDictionary = [NSMutableDictionary dictionary];
}

/**
 *  创建订阅号
 *
 *  @param subscriptionNumber 订阅号码
 */
- (void)createSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = _subscriptionNumberDictionary[subscriptionNumber];
    if (hashTable == nil) {
        hashTable = [NSHashTable weakObjectsHashTable];
        [_subscriptionNumberDictionary setObject:hashTable forKey:subscriptionNumber];
    }
}

/**
 *  移除订阅号(参与到该订阅号码的所有客户不会再收到订阅信息)
 *
 *  @param subscriptionNumber 订阅号码
 */
- (void)removeSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = _subscriptionNumberDictionary[subscriptionNumber];
    if (hashTable) {
        [_subscriptionNumberDictionary removeObjectForKey:subscriptionNumber];
    }
}

/**
 *  客户订阅指定的订阅号
 *
 *  @param customer           客户对象
 *  @param subscriptionNumber 订阅号码
 */
- (void)addCustomer:(id<SubscriptionProtocol>)customer
withSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = _subscriptionNumberDictionary[subscriptionNumber];
    if (hashTable && customer) {
        [hashTable addObject:customer];
    }
}

/**
 *  将指定客户从指定订阅号上移除掉
 *
 *  @param customer           客户对象
 *  @param subscriptionNumber 订阅号码
 */
- (void)removeCustomer:(id<SubscriptionProtocol>)customer
fromSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = _subscriptionNumberDictionary[subscriptionNumber];
    if (hashTable && customer) {
        [hashTable removeObject:customer];
    }
}

/**
 *  通知签订了订阅号码的对象
 *
 *  @param message            信息对象
 *  @param subscriptionNumber 订阅号码
 */
- (void)sendMessage:(id)message
toSubscriptionNumber:(NSString *)subscriptionNumber {
    NSParameterAssert(subscriptionNumber);
    NSHashTable *hashTable = _subscriptionNumberDictionary[subscriptionNumber];
    for (id<SubscriptionProtocol> customer in hashTable) {
        if ([customer respondsToSelector:@selector(subscriptionMessage:subscriptionNumber:)]) {
            [customer subscriptionMessage:message subscriptionNumber:subscriptionNumber];
        }
    }
}

@end

SubscriptionServerCenter *center = [[SubscriptionServerCenter alloc] init];
[center createSubscriptionNumber:@"COMPUTER"];
[center addCustomer:self withSubscriptionNumber:@"COMPUTER"];
[center sendMessage:@"apple" toSubscriptionNumber:@"COMPUTER"];
上一篇 下一篇

猜你喜欢

热点阅读