Skip to content

Commit ab527e9

Browse files
committed
享元模式完成。
1 parent bb1cd44 commit ab527e9

12 files changed

Lines changed: 187 additions & 4 deletions

File tree

21享元模式/21享元模式.xcodeproj/project.pbxproj

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
DD2D69AE1B7F40C000F9E848 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DD2D69AD1B7F40C000F9E848 /* Images.xcassets */; };
1515
DD2D69B11B7F40C000F9E848 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = DD2D69AF1B7F40C000F9E848 /* LaunchScreen.xib */; };
1616
DD2D69BD1B7F40C000F9E848 /* _1____Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD2D69BC1B7F40C000F9E848 /* _1____Tests.m */; };
17+
DDEA4B051B8EFF4600DF9EB5 /* 享元模式.png in Resources */ = {isa = PBXBuildFile; fileRef = DDEA4B041B8EFF4600DF9EB5 /* 享元模式.png */; };
18+
DDEA4B081B8F001100DF9EB5 /* HCDUser.m in Sources */ = {isa = PBXBuildFile; fileRef = DDEA4B071B8F001100DF9EB5 /* HCDUser.m */; };
19+
DDEA4B0C1B8F008E00DF9EB5 /* HCDConcreteWebSite.m in Sources */ = {isa = PBXBuildFile; fileRef = DDEA4B0B1B8F008E00DF9EB5 /* HCDConcreteWebSite.m */; };
20+
DDEA4B0F1B8F012E00DF9EB5 /* HCDWebSiteFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = DDEA4B0E1B8F012E00DF9EB5 /* HCDWebSiteFactory.m */; };
1721
/* End PBXBuildFile section */
1822

1923
/* Begin PBXContainerItemProxy section */
@@ -40,6 +44,14 @@
4044
DD2D69B61B7F40C000F9E848 /* 21享元模式Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "21享元模式Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
4145
DD2D69BB1B7F40C000F9E848 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4246
DD2D69BC1B7F40C000F9E848 /* _1____Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "_1____Tests.m"; sourceTree = "<group>"; };
47+
DDEA4B041B8EFF4600DF9EB5 /* 享元模式.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "享元模式.png"; sourceTree = "<group>"; };
48+
DDEA4B061B8F001100DF9EB5 /* HCDUser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDUser.h; sourceTree = "<group>"; };
49+
DDEA4B071B8F001100DF9EB5 /* HCDUser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDUser.m; sourceTree = "<group>"; };
50+
DDEA4B091B8F004300DF9EB5 /* HCDWebSite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDWebSite.h; sourceTree = "<group>"; };
51+
DDEA4B0A1B8F008E00DF9EB5 /* HCDConcreteWebSite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDConcreteWebSite.h; sourceTree = "<group>"; };
52+
DDEA4B0B1B8F008E00DF9EB5 /* HCDConcreteWebSite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDConcreteWebSite.m; sourceTree = "<group>"; };
53+
DDEA4B0D1B8F012E00DF9EB5 /* HCDWebSiteFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCDWebSiteFactory.h; sourceTree = "<group>"; };
54+
DDEA4B0E1B8F012E00DF9EB5 /* HCDWebSiteFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HCDWebSiteFactory.m; sourceTree = "<group>"; };
4355
/* End PBXFileReference section */
4456

4557
/* Begin PBXFrameworksBuildPhase section */
@@ -81,6 +93,8 @@
8193
DD2D699F1B7F40C000F9E848 /* 21享元模式 */ = {
8294
isa = PBXGroup;
8395
children = (
96+
DDEA4B031B8EFF3100DF9EB5 /* 享元模式 */,
97+
DDEA4B041B8EFF4600DF9EB5 /* 享元模式.png */,
8498
DD2D69A41B7F40C000F9E848 /* AppDelegate.h */,
8599
DD2D69A51B7F40C000F9E848 /* AppDelegate.m */,
86100
DD2D69A71B7F40C000F9E848 /* ViewController.h */,
@@ -119,6 +133,20 @@
119133
name = "Supporting Files";
120134
sourceTree = "<group>";
121135
};
136+
DDEA4B031B8EFF3100DF9EB5 /* 享元模式 */ = {
137+
isa = PBXGroup;
138+
children = (
139+
DDEA4B061B8F001100DF9EB5 /* HCDUser.h */,
140+
DDEA4B071B8F001100DF9EB5 /* HCDUser.m */,
141+
DDEA4B091B8F004300DF9EB5 /* HCDWebSite.h */,
142+
DDEA4B0A1B8F008E00DF9EB5 /* HCDConcreteWebSite.h */,
143+
DDEA4B0B1B8F008E00DF9EB5 /* HCDConcreteWebSite.m */,
144+
DDEA4B0D1B8F012E00DF9EB5 /* HCDWebSiteFactory.h */,
145+
DDEA4B0E1B8F012E00DF9EB5 /* HCDWebSiteFactory.m */,
146+
);
147+
name = "享元模式";
148+
sourceTree = "<group>";
149+
};
122150
/* End PBXGroup section */
123151

124152
/* Begin PBXNativeTarget section */
@@ -200,6 +228,7 @@
200228
buildActionMask = 2147483647;
201229
files = (
202230
DD2D69AC1B7F40C000F9E848 /* Main.storyboard in Resources */,
231+
DDEA4B051B8EFF4600DF9EB5 /* 享元模式.png in Resources */,
203232
DD2D69B11B7F40C000F9E848 /* LaunchScreen.xib in Resources */,
204233
DD2D69AE1B7F40C000F9E848 /* Images.xcassets in Resources */,
205234
);
@@ -219,8 +248,11 @@
219248
isa = PBXSourcesBuildPhase;
220249
buildActionMask = 2147483647;
221250
files = (
251+
DDEA4B0C1B8F008E00DF9EB5 /* HCDConcreteWebSite.m in Sources */,
222252
DD2D69A91B7F40C000F9E848 /* ViewController.m in Sources */,
223253
DD2D69A61B7F40C000F9E848 /* AppDelegate.m in Sources */,
254+
DDEA4B081B8F001100DF9EB5 /* HCDUser.m in Sources */,
255+
DDEA4B0F1B8F012E00DF9EB5 /* HCDWebSiteFactory.m in Sources */,
224256
DD2D69A31B7F40C000F9E848 /* main.m in Sources */,
225257
);
226258
runOnlyForDeploymentPostprocessing = 0;
@@ -416,6 +448,7 @@
416448
DD2D69C21B7F40C000F9E848 /* Release */,
417449
);
418450
defaultConfigurationIsVisible = 0;
451+
defaultConfigurationName = Release;
419452
};
420453
DD2D69C31B7F40C000F9E848 /* Build configuration list for PBXNativeTarget "21享元模式Tests" */ = {
421454
isa = XCConfigurationList;
@@ -424,6 +457,7 @@
424457
DD2D69C51B7F40C000F9E848 /* Release */,
425458
);
426459
defaultConfigurationIsVisible = 0;
460+
defaultConfigurationName = Release;
427461
};
428462
/* End XCConfigurationList section */
429463
};

21享元模式/21享元模式.xcodeproj/project.xcworkspace/xcshareddata/21享元模式.xccheckout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<key>IDESourceControlProjectIdentifier</key>
88
<string>DD8853CD-1477-4808-A36E-AC89F9D12F65</string>
99
<key>IDESourceControlProjectName</key>
10-
<string>project</string>
10+
<string>21享元模式</string>
1111
<key>IDESourceControlProjectOriginsDictionary</key>
1212
<dict>
1313
<key>97B4AD1D63A96A3E6D01F6FF529543E2092CC1C1</key>
1414
<string>https://github.com/huang303513/Design-Pattern-For-iOS.git</string>
1515
</dict>
1616
<key>IDESourceControlProjectPath</key>
17-
<string>21享元模式/21享元模式.xcodeproj/project.xcworkspace</string>
17+
<string>21享元模式/21享元模式.xcodeproj</string>
1818
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
1919
<dict>
2020
<key>97B4AD1D63A96A3E6D01F6FF529543E2092CC1C1</key>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDConcreteWebSite.h
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "HCDWebSite.h"
11+
@interface HCDConcreteWebSite : NSObject<HCDWebSite>
12+
@property(nonatomic,strong)NSString *webName;
13+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// HCDConcreteWebSite.m
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDConcreteWebSite.h"
10+
11+
@implementation HCDConcreteWebSite
12+
-(void)use:(HCDUser *)user{
13+
NSLog(@"网站分类:%@,用户:%@",self.webName,user.name);
14+
}
15+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDUser.h
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface HCDUser : NSObject
12+
@property(nonatomic,strong)NSString *name;
13+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDUser.m
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDUser.h"
10+
11+
@implementation HCDUser
12+
13+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HCDWebSite.h
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "HCDUser.h"
11+
@protocol HCDWebSite <NSObject>
12+
-(void)use:(HCDUser *)user;
13+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// HCDWebSiteFactory.h
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "HCDWebSite.h"
11+
12+
13+
@interface HCDWebSiteFactory : NSObject
14+
@property(nonatomic,strong)NSDictionary *flyweights;
15+
16+
-(id<HCDWebSite> )getWebSiteCategory:(NSString *)webkey;
17+
-(NSInteger)getWebSiteCount;
18+
@end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// HCDWebSiteFactory.m
3+
// 21享元模式
4+
//
5+
// Created by yifan on 15/8/27.
6+
// Copyright (c) 2015年 黄成都. All rights reserved.
7+
//
8+
9+
#import "HCDWebSiteFactory.h"
10+
#import "HCDConcreteWebSite.h"
11+
@implementation HCDWebSiteFactory
12+
13+
-(instancetype)init{
14+
self = [super init];
15+
if (self) {
16+
_flyweights = [NSDictionary dictionary];
17+
}
18+
return self;
19+
}
20+
21+
-(id<HCDWebSite> )getWebSiteCategory:(NSString *)webkey{
22+
__block id<HCDWebSite> webset = nil;
23+
[self.flyweights enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
24+
if (webkey == key) {
25+
webset = obj;
26+
*stop = YES;
27+
}
28+
}];
29+
if (webset == nil) {
30+
HCDConcreteWebSite *concreteset = [[HCDConcreteWebSite alloc] init];
31+
concreteset.webName = webkey;
32+
webset = concreteset;
33+
34+
NSMutableDictionary *mutabledic = [NSMutableDictionary dictionaryWithDictionary:self.flyweights];
35+
[mutabledic setObject:webset forKey:webkey];
36+
self.flyweights = [NSDictionary dictionaryWithDictionary:mutabledic];
37+
}
38+
return webset;
39+
}
40+
41+
-(NSInteger)getWebSiteCount{
42+
return self.flyweights.count;
43+
}
44+
@end

0 commit comments

Comments
 (0)