iOS架构师_观察者模式
定义:
观察者模式(有时又被称为模型-视图(View)模式、源-收听者(Listener)模式或从属者模式),一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实现事件处理系统。
示例代码:自己实现观察者模式
创建Protocol类型的接口文件SubProtocol协议类
SubProtocol.h
1 | #import <Foundation/Foundation.h> |
2 | |
3 | @protocol SubProtocol <NSObject> |
4 | |
5 | //用户的信息和从某一个刊物接收的信息 |
6 | – (void)subMessage:(id)message withSubNumber:(NSString *)withSubNumber; |
7 | @end |
创建观察中心SubCenter类
SubCenter.h
1 | #import <Foundation/Foundation.h> |
2 | #import “SubProtocol.h” |
3 | |
4 | @interface SubCenter : NSObject |
5 | // 创建书刊订阅号 |
6 | + (void)creatNumber:(NSString *)subNumber; |
7 | |
8 | // 移除订阅号 |
9 | + (void)removeNumber:(NSString *)subNumber; |
10 | |
11 | // 添加用户 |
12 | + (void)addUser:(id <SubProtocol>)user wihtNumber:(NSString *)userNumber; |
13 | |
14 | // 移除用户 |
15 | + (void)removeUser:(id <SubProtocol>)user withNumber:(NSString *)userNumber; |
16 | |
17 | // 发送消息 |
18 | + (void)sendMessage:(id)message withSubNumber:(NSString *)SubNumber; |
19 | |
20 | @end |
SubCenter.m
1 | // |
2 | // SubCenter.m |
3 | // 观察者模式 |
4 | // |
5 | // Created by James on 2017/12/9. |
6 | // Copyright © 2017年 TZ. All rights reserved. |
7 | |
8 | |
9 | #import “SubCenter.h” |
10 | |
11 | |
12 | static NSMutableDictionary *bookCenter = nil; |
13 | @implementation SubCenter |
14 | |
15 | //单例 |
16 | + (void)initialize { |
17 | if (self == [SubCenter class]) { |
18 | bookCenter = [NSMutableDictionary dictionary]; |
19 | } |
20 | } |
21 | |
22 | // 创建书刊订阅号 |
23 | + (void)creatNumber:(NSString *)subNumber { |
24 | // NSHashTable 就是一个集合,但是它是弱引用的. |
25 | NSHashTable *hashTable = [self existNumber:subNumber]; |
26 | if (hashTable == nil) { |
27 | hashTable = [NSHashTable weakObjectsHashTable]; |
28 | [bookCenter setObject:hashTable forKey:subNumber]; |
29 | } |
30 | } |
31 | |
32 | // 移除订阅号 |
33 | + (void)removeNumber:(NSString *)subNumber { |
34 | NSHashTable *hashTable = [self existNumber:subNumber]; |
35 | if (hashTable) { |
36 | [bookCenter removeObjectForKey:subNumber]; |
37 | } |
38 | } |
39 | |
40 | // 添加用户 |
41 | + (void)addUser:(id <SubProtocol>)user wihtNumber:(NSString *)userNumber { |
42 | NSHashTable *hashTable = [self existNumber:userNumber]; |
43 | [hashTable addObject:user]; |
44 | } |
45 | |
46 | // 移除用户 |
47 | + (void)removeUser:(id <SubProtocol>)user withNumber:(NSString *)userNumber { |
48 | NSHashTable *hashTable = [self existNumber:userNumber]; |
49 | [hashTable removeObject:user]; |
50 | } |
51 | |
52 | // 发送消息 |
53 | + (void)sendMessage:(id)message withSubNumber:(NSString *)SubNumber { |
54 | NSHashTable *hashTable = [self existNumber:SubNumber]; |
55 | if (hashTable) { |
56 | //构造器 |
57 | NSEnumerator *enumerato = [hashTable objectEnumerator]; |
58 | id <SubProtocol> object = nil; |
59 | |
60 | while (object = [enumerato nextObject]) { |
61 | if ([object respondsToSelector:@selector(subMessage:withSubNumber:)]) { |
62 | [object subMessage:message withSubNumber:SubNumber]; |
63 | } |
64 | } |
65 | |
66 | } |
67 | } |
68 | |
69 | #pragma mark – 私有方法 |
70 | // 实现了代理方法 |
71 | + (NSHashTable *)existNumber:(NSString *)subStringNumber { |
72 | return [bookCenter objectForKey:subStringNumber]; |
73 | } |
74 | |
75 | @end |
ViewController调用
1 | #import “ViewController.h” |
2 | #import “SubCenter.h” |
3 | |
4 | |
5 | static NSString *SCIENCE = @”SCIENCE”; |
6 | @interface ViewController () <SubProtocol> |
7 | |
8 | @end |
9 | |
10 | @implementation ViewController |
11 | |
12 | – (void)viewDidLoad { |
13 | [super viewDidLoad]; |
14 | // Do any additional setup after loading the view, typically from a nib. |
15 | // 创建订阅 |
16 | [SubCenter creatNumber:SCIENCE]; |
17 | |
18 | // 添加订阅 |
19 | [SubCenter addUser:self wihtNumber:SCIENCE]; |
20 | |
21 | // 发送消息 |
22 | [SubCenter sendMessage:@”11″ withSubNumber:SCIENCE]; |
23 | } |
24 | |
25 | – (void)subMessage:(id)message withSubNumber:(NSString *)withSubNumber { |
26 | NSLog(@”—-%@—-%@”,message, withSubNumber); |
27 | } |
28 | |
29 | |
30 | @end |