Skip to content

Commit d4b9187

Browse files
committed
Observer
Observer
1 parent 7854da9 commit d4b9187

File tree

8 files changed

+132
-1
lines changed

8 files changed

+132
-1
lines changed

MLDesignPatterns-OC/MLDesignPatterns-OC.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
10428FF31F8D22E800C67F26 /* Command.m in Sources */ = {isa = PBXBuildFile; fileRef = 10428FF21F8D22E800C67F26 /* Command.m */; };
1414
10428FF71F8FC3AD00C67F26 /* ChatRoom.m in Sources */ = {isa = PBXBuildFile; fileRef = 10428FF61F8FC3AD00C67F26 /* ChatRoom.m */; };
1515
10428FFA1F8FC4A600C67F26 /* User.m in Sources */ = {isa = PBXBuildFile; fileRef = 10428FF91F8FC4A600C67F26 /* User.m */; };
16+
10428FFD1F8FCB4300C67F26 /* JobProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 10428FFC1F8FCB4300C67F26 /* JobProvider.m */; };
17+
104290051F8FD05700C67F26 /* JobHunter.m in Sources */ = {isa = PBXBuildFile; fileRef = 104290041F8FD05700C67F26 /* JobHunter.m */; };
1618
104D30E61F5C5CC30092D139 /* ChickenBurger.m in Sources */ = {isa = PBXBuildFile; fileRef = 104D30E51F5C5CC30092D139 /* ChickenBurger.m */; };
1719
104D30E91F5C5DA60092D139 /* Condiment.m in Sources */ = {isa = PBXBuildFile; fileRef = 104D30E81F5C5DA60092D139 /* Condiment.m */; };
1820
104D30EC1F5C5E290092D139 /* Chilli.m in Sources */ = {isa = PBXBuildFile; fileRef = 104D30EB1F5C5E290092D139 /* Chilli.m */; };
@@ -96,6 +98,12 @@
9698
10428FF61F8FC3AD00C67F26 /* ChatRoom.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ChatRoom.m; sourceTree = "<group>"; };
9799
10428FF81F8FC4A600C67F26 /* User.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = User.h; sourceTree = "<group>"; };
98100
10428FF91F8FC4A600C67F26 /* User.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = User.m; sourceTree = "<group>"; };
101+
10428FFB1F8FCB4300C67F26 /* JobProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JobProvider.h; sourceTree = "<group>"; };
102+
10428FFC1F8FCB4300C67F26 /* JobProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JobProvider.m; sourceTree = "<group>"; };
103+
10428FFE1F8FCD5B00C67F26 /* ObserverProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ObserverProtocol.h; sourceTree = "<group>"; };
104+
104290021F8FCF3D00C67F26 /* SubjectProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SubjectProtocol.h; sourceTree = "<group>"; };
105+
104290031F8FD05700C67F26 /* JobHunter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JobHunter.h; sourceTree = "<group>"; };
106+
104290041F8FD05700C67F26 /* JobHunter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JobHunter.m; sourceTree = "<group>"; };
99107
104D30E31F5C5C740092D139 /* Humburger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Humburger.h; sourceTree = "<group>"; };
100108
104D30E41F5C5CC30092D139 /* ChickenBurger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ChickenBurger.h; sourceTree = "<group>"; };
101109
104D30E51F5C5CC30092D139 /* ChickenBurger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ChickenBurger.m; sourceTree = "<group>"; };
@@ -504,6 +512,12 @@
504512
107938B31F3B761F000F287A /* Observer */ = {
505513
isa = PBXGroup;
506514
children = (
515+
104290021F8FCF3D00C67F26 /* SubjectProtocol.h */,
516+
10428FFB1F8FCB4300C67F26 /* JobProvider.h */,
517+
10428FFC1F8FCB4300C67F26 /* JobProvider.m */,
518+
10428FFE1F8FCD5B00C67F26 /* ObserverProtocol.h */,
519+
104290031F8FD05700C67F26 /* JobHunter.h */,
520+
104290041F8FD05700C67F26 /* JobHunter.m */,
507521
);
508522
path = Observer;
509523
sourceTree = "<group>";
@@ -661,6 +675,7 @@
661675
109400481F42025E0026A0BB /* Singleton.m in Sources */,
662676
104D30F61F5DB19F0092D139 /* FlyweightFactory.m in Sources */,
663677
104D31041F619F8B0092D139 /* Manager.m in Sources */,
678+
10428FFD1F8FCB4300C67F26 /* JobProvider.m in Sources */,
664679
10D385371F4497120095A417 /* YellowHuman.m in Sources */,
665680
104D30F31F5DB1010092D139 /* Circle.m in Sources */,
666681
10428FF31F8D22E800C67F26 /* Command.m in Sources */,
@@ -687,6 +702,7 @@
687702
10D385EF1F4B41BF0095A417 /* FiveYuanNoSpicyBuild.m in Sources */,
688703
10D385361F4497120095A417 /* WhiteHuman.m in Sources */,
689704
10428FEA1F8D21CB00C67F26 /* Cook.m in Sources */,
705+
104290051F8FD05700C67F26 /* JobHunter.m in Sources */,
690706
10D385DC1F4B32870095A417 /* WoodDoorFactory.m in Sources */,
691707
10428FF71F8FC3AD00C67F26 /* ChatRoom.m in Sources */,
692708
104D30EC1F5C5E290092D139 /* Chilli.m in Sources */,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// JobHunter.h
3+
// MLDesignPatterns-OC
4+
//
5+
// Created by mjpc on 2017/10/13.
6+
// Copyright © 2017年 mali. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface JobHunter : NSObject
12+
13+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// JobHunter.m
3+
// MLDesignPatterns-OC
4+
//
5+
// Created by mjpc on 2017/10/13.
6+
// Copyright © 2017年 mali. All rights reserved.
7+
//
8+
9+
#import "JobHunter.h"
10+
11+
@implementation JobHunter
12+
13+
@end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Subject.h
3+
// MLDesignPatterns-OC
4+
//
5+
// Created by mjpc on 2017/10/13.
6+
// Copyright © 2017年 mali. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "SubjectProtocol.h"
11+
12+
@interface Subject : NSObject<SubjectProtocol>
13+
14+
@end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Subject.m
3+
// MLDesignPatterns-OC
4+
//
5+
// Created by mjpc on 2017/10/13.
6+
// Copyright © 2017年 mali. All rights reserved.
7+
//
8+
9+
#import "Subject.h"
10+
11+
@interface Subject()
12+
13+
@property (nonatomic, strong) NSMutableArray *observers;
14+
15+
@end
16+
17+
@implementation Subject
18+
19+
- (void)addObserver:(NSObject *)observer {
20+
[self.observers addObject:observer];
21+
}
22+
- (void)removeObserver:(NSObject *)observer {
23+
[self.observers removeObject:observer];
24+
}
25+
- (void)notify {
26+
for (id <ObserverProtocol> observer in self.observers) {
27+
[observer update];
28+
}
29+
}
30+
31+
- (NSMutableArray *)observers {
32+
if (!_observers) {
33+
_observers = [[NSMutableArray alloc] init];
34+
}
35+
return _observers;
36+
}
37+
38+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ObserverProtocol.h
3+
// MLDesignPatterns-OC
4+
//
5+
// Created by mjpc on 2017/10/13.
6+
// Copyright © 2017年 mali. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@protocol ObserverProtocol <NSObject>
12+
13+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// SubjectProtocol.h
3+
// MLDesignPatterns-OC
4+
//
5+
// Created by mjpc on 2017/10/13.
6+
// Copyright © 2017年 mali. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@protocol SubjectProtocol <NSObject>
12+
13+
@end

MLDesignPatterns-OC/MLDesignPatterns-OC/main.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#import "AbaloneCommand.h"
4545
#import "User.h"
4646
#import "ChatRoom.h"
47+
#import "JobProvider.h"
48+
#import "JobHunter.h"
4749

4850
#warning 调用部分请见main方法
4951

@@ -200,6 +202,14 @@ void mediator() {
200202
[me sendMessage:@"谢谢,不做伪工作者"];
201203
}
202204

205+
void observer() {
206+
JobHunter *zhangSan = [[JobHunter alloc] initWithName:@"张三"];
207+
JobHunter *liSi = [[JobHunter alloc] initWithName:@"李四"];
208+
JobProvider *jobProvider = [[JobProvider alloc] init];
209+
[jobProvider addObserver:zhangSan];
210+
[jobProvider addObserver:liSi];
211+
[jobProvider notify];
212+
}
203213

204214
int main(int argc, const char * argv[]) {
205215
@autoreleasepool {
@@ -217,7 +227,8 @@ int main(int argc, const char * argv[]) {
217227
// proxy();
218228
// chainOfResponsibility();
219229
// command();
220-
mediator();
230+
// mediator();
231+
observer();
221232
}
222233
return 0;
223234
}

0 commit comments

Comments
 (0)