Skip to content

Commit b101615

Browse files
committed
观察者模式有bug
1 parent 1c3b6f4 commit b101615

13 files changed

Lines changed: 217 additions & 1 deletion

File tree

9观察者模式/9观察者模式.xcodeproj/project.pbxproj

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
DDBEA9B61B7C87BF004874A1 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DDBEA9B51B7C87BF004874A1 /* Images.xcassets */; };
1515
DDBEA9B91B7C87BF004874A1 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = DDBEA9B71B7C87BF004874A1 /* LaunchScreen.xib */; };
1616
DDBEA9C51B7C87BF004874A1 /* ______Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDBEA9C41B7C87BF004874A1 /* ______Tests.m */; };
17+
DDBEA9CF1B7C8C44004874A1 /* 观察者模式类图.png in Resources */ = {isa = PBXBuildFile; fileRef = DDBEA9CE1B7C8C44004874A1 /* 观察者模式类图.png */; };
18+
DDBEA9D41B7C9C36004874A1 /* HCDSubject.m in Sources */ = {isa = PBXBuildFile; fileRef = DDBEA9D31B7C9C36004874A1 /* HCDSubject.m */; };
19+
DDBEA9D81B7C9D19004874A1 /* HCDSecreteSubject.m in Sources */ = {isa = PBXBuildFile; fileRef = DDBEA9D71B7C9D19004874A1 /* HCDSecreteSubject.m */; };
20+
DDBEA9DB1B7C9EC2004874A1 /* HCDStockObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DDBEA9DA1B7C9EC2004874A1 /* HCDStockObserver.m */; };
21+
DDBEA9DE1B7C9F18004874A1 /* HCDNBAObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DDBEA9DD1B7C9F18004874A1 /* HCDNBAObserver.m */; };
1722
/* End PBXBuildFile section */
1823

1924
/* Begin PBXContainerItemProxy section */
@@ -40,6 +45,16 @@
4045
DDBEA9BE1B7C87BF004874A1 /* 9观察者模式Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "9观察者模式Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
4146
DDBEA9C31B7C87BF004874A1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4247
DDBEA9C41B7C87BF004874A1 /* ______Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "______Tests.m"; sourceTree = "<group>"; };
48+
DDBEA9CE1B7C8C44004874A1 /* 观察者模式类图.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "观察者模式类图.png"; sourceTree = "<group>"; };
49+
DDBEA9D21B7C9C36004874A1 /* HCDSubject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDSubject.h; sourceTree = "<group>"; };
50+
DDBEA9D31B7C9C36004874A1 /* HCDSubject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDSubject.m; sourceTree = "<group>"; };
51+
DDBEA9D51B7C9C79004874A1 /* HCDObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDObserver.h; sourceTree = "<group>"; };
52+
DDBEA9D61B7C9D19004874A1 /* HCDSecreteSubject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDSecreteSubject.h; sourceTree = "<group>"; };
53+
DDBEA9D71B7C9D19004874A1 /* HCDSecreteSubject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDSecreteSubject.m; sourceTree = "<group>"; };
54+
DDBEA9D91B7C9EC2004874A1 /* HCDStockObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDStockObserver.h; sourceTree = "<group>"; };
55+
DDBEA9DA1B7C9EC2004874A1 /* HCDStockObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDStockObserver.m; sourceTree = "<group>"; };
56+
DDBEA9DC1B7C9F18004874A1 /* HCDNBAObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDNBAObserver.h; sourceTree = "<group>"; };
57+
DDBEA9DD1B7C9F18004874A1 /* HCDNBAObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDNBAObserver.m; sourceTree = "<group>"; };
4358
/* End PBXFileReference section */
4459

4560
/* Begin PBXFrameworksBuildPhase section */
@@ -81,6 +96,8 @@
8196
DDBEA9A71B7C87BF004874A1 /* 9观察者模式 */ = {
8297
isa = PBXGroup;
8398
children = (
99+
DDBEA9D01B7C9BE2004874A1 /* 观察者模式 */,
100+
DDBEA9CE1B7C8C44004874A1 /* 观察者模式类图.png */,
84101
DDBEA9AC1B7C87BF004874A1 /* AppDelegate.h */,
85102
DDBEA9AD1B7C87BF004874A1 /* AppDelegate.m */,
86103
DDBEA9AF1B7C87BF004874A1 /* ViewController.h */,
@@ -119,6 +136,22 @@
119136
name = "Supporting Files";
120137
sourceTree = "<group>";
121138
};
139+
DDBEA9D01B7C9BE2004874A1 /* 观察者模式 */ = {
140+
isa = PBXGroup;
141+
children = (
142+
DDBEA9D21B7C9C36004874A1 /* HCDSubject.h */,
143+
DDBEA9D31B7C9C36004874A1 /* HCDSubject.m */,
144+
DDBEA9D51B7C9C79004874A1 /* HCDObserver.h */,
145+
DDBEA9D61B7C9D19004874A1 /* HCDSecreteSubject.h */,
146+
DDBEA9D71B7C9D19004874A1 /* HCDSecreteSubject.m */,
147+
DDBEA9D91B7C9EC2004874A1 /* HCDStockObserver.h */,
148+
DDBEA9DA1B7C9EC2004874A1 /* HCDStockObserver.m */,
149+
DDBEA9DC1B7C9F18004874A1 /* HCDNBAObserver.h */,
150+
DDBEA9DD1B7C9F18004874A1 /* HCDNBAObserver.m */,
151+
);
152+
name = "观察者模式";
153+
sourceTree = "<group>";
154+
};
122155
/* End PBXGroup section */
123156

124157
/* Begin PBXNativeTarget section */
@@ -200,6 +233,7 @@
200233
buildActionMask = 2147483647;
201234
files = (
202235
DDBEA9B41B7C87BF004874A1 /* Main.storyboard in Resources */,
236+
DDBEA9CF1B7C8C44004874A1 /* 观察者模式类图.png in Resources */,
203237
DDBEA9B91B7C87BF004874A1 /* LaunchScreen.xib in Resources */,
204238
DDBEA9B61B7C87BF004874A1 /* Images.xcassets in Resources */,
205239
);
@@ -221,7 +255,11 @@
221255
files = (
222256
DDBEA9B11B7C87BF004874A1 /* ViewController.m in Sources */,
223257
DDBEA9AE1B7C87BF004874A1 /* AppDelegate.m in Sources */,
258+
DDBEA9DB1B7C9EC2004874A1 /* HCDStockObserver.m in Sources */,
259+
DDBEA9DE1B7C9F18004874A1 /* HCDNBAObserver.m in Sources */,
260+
DDBEA9D41B7C9C36004874A1 /* HCDSubject.m in Sources */,
224261
DDBEA9AB1B7C87BF004874A1 /* main.m in Sources */,
262+
DDBEA9D81B7C9D19004874A1 /* HCDSecreteSubject.m in Sources */,
225263
);
226264
runOnlyForDeploymentPostprocessing = 0;
227265
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDNBAObserver.h
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "HCDObserver.h"
11+
@interface HCDNBAObserver : NSObject<HCDObserver>
12+
13+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// HCDNBAObserver.m
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDNBAObserver.h"
10+
11+
@implementation HCDNBAObserver
12+
-(void)update{
13+
NSLog(@"擦,老板回来了,待会儿继续看NBA");
14+
}
15+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDObserver.h
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@protocol HCDObserver <NSObject>
12+
-(void)update;
13+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDSecreteSubject.h
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDSubject.h"
10+
11+
@interface HCDSecreteSubject : HCDSubject
12+
13+
@end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// HCDSecreteSubject.m
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDSecreteSubject.h"
10+
#import "HCDObserver.h"
11+
12+
@implementation HCDSecreteSubject
13+
-(void)notify{
14+
NSLog(@"秘书通知:老板回来了,大家赶紧撤");
15+
for (id<HCDObserver> observer in self.observerList) {
16+
[observer update];
17+
}
18+
}
19+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDStockObserver.h
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "HCDObserver.h"
11+
@interface HCDStockObserver : NSObject<HCDObserver>
12+
13+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// HCDStockObserver.m
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDStockObserver.h"
10+
11+
@implementation HCDStockObserver
12+
-(void)update{
13+
NSLog(@"操。。老板回来了,带会儿再看");
14+
}
15+
@end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// HCDSubject.h
3+
// 9观察者模式
4+
//
5+
// Created by yifan on 15/8/13.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "HCDObserver.h"
11+
@interface HCDSubject : NSObject
12+
//这里很不合理,不知怎么办
13+
@property(nonatomic,readwrite,strong)NSMutableArray *observerList;
14+
-(void)attach:(id<HCDObserver>)observer;
15+
-(void)detach:(id<HCDObserver>)observer;
16+
-(void)notify;
17+
@end

0 commit comments

Comments
 (0)