-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubscriptionServe.m
68 lines (59 loc) · 2.39 KB
/
SubscriptionServe.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// SubscriptionServe.m
// OberservePattern
//
// Created by cq on 16/1/30.
// Copyright © 2016年 songsong. All rights reserved.
//
#import "SubscriptionServe.h"
static NSMutableDictionary *_subscriptionDictionary=nil;
@implementation SubscriptionServe
+ (void)initialize {
if ((self == [SubscriptionServe class])) {
_subscriptionDictionary=[NSMutableDictionary dictionary];
}
}
+ (void)createSubscriptionNumber:(NSString *) subscriptionNumber{
NSParameterAssert(subscriptionNumber);
NSHashTable *hashTable =[self existSubscriptinNumber:subscriptionNumber];
if (hashTable == nil) {
hashTable=[NSHashTable weakObjectsHashTable];
[_subscriptionDictionary setObject:hashTable forKey:subscriptionNumber];
}
}
+ (void)removeSubscriptinNumber:(NSString *) subscriptionNumber{
NSParameterAssert(subscriptionNumber);
NSHashTable *hashTable =[self existSubscriptinNumber:subscriptionNumber];
if (hashTable == nil) {
hashTable=[NSHashTable weakObjectsHashTable];
[_subscriptionDictionary removeObjectForKey:subscriptionNumber];
}
}
+ (void)addCustom:(id<SubscriptionServeProtocol>)custom withSubscriptinNumber:(NSString *) subscriptionNumber{
NSParameterAssert(custom);
NSParameterAssert(subscriptionNumber);
NSHashTable *hashTable=[self existSubscriptinNumber:subscriptionNumber];
[hashTable addObject:custom];
}
+ (void)removeCustom:(id<SubscriptionServeProtocol>)custom withSubscriptinNumber:(NSString *) subscriptionNumber{
NSParameterAssert(subscriptionNumber);
NSHashTable *hashTable=[self existSubscriptinNumber:subscriptionNumber];
[hashTable removeObject:custom];
}
+ (void)sendMessage:(id)message toSubscriptinNumber:(NSString *) subscriptionNumber{
NSParameterAssert(subscriptionNumber);
NSHashTable *hashTable=[self existSubscriptinNumber:subscriptionNumber];
if (hashTable) {
NSEnumerator *enimerator =[hashTable objectEnumerator];
id<SubscriptionServeProtocol> object=nil;
while (object =[enimerator nextObject]) {
if ([object respondsToSelector:@selector(subscriptionMessage:SubscriptinNumber:)]) {
[object subscriptionMessage:message SubscriptinNumber:subscriptionNumber];
}
}
}
}
+ (NSHashTable *)existSubscriptinNumber:(NSString *) subscriptionNumber{
return [_subscriptionDictionary objectForKey:subscriptionNumber];
}
@end