Skip to content

Commit 708e389

Browse files
composite 模式
1 parent 6fe5f84 commit 708e389

File tree

12 files changed

+610
-0
lines changed

12 files changed

+610
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 组合模式
2+
3+
## 什么是组合模式?
4+
将对象组合成树形结构,以表示**“部分-整体”**的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性
5+
6+
## 组合模式有哪些优点?
7+
* 灵活性高,比较容易增加移除构件
8+
* 高层模块调用简单,调用整体和调用局部没有区别
9+
10+
## 组合模式有哪些缺点?
11+
* 与依赖倒置原则冲突
12+
13+
## 适用场景
14+
* 需求中是体现部分与整体层次的结构时
15+
* 希望用户可以忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时
16+
17+
18+
Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 50;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
DA7BA25921D507C900956679 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA25821D507C900956679 /* main.m */; };
11+
DA7BA26321D508E200956679 /* ConcreteCompany.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA26221D508E200956679 /* ConcreteCompany.m */; };
12+
DA7BA26621D5090900956679 /* HRDepartment.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA26521D5090900956679 /* HRDepartment.m */; };
13+
DA7BA26921D5092200956679 /* FinanceDepartment.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA26821D5092200956679 /* FinanceDepartment.m */; };
14+
/* End PBXBuildFile section */
15+
16+
/* Begin PBXCopyFilesBuildPhase section */
17+
DA7BA25321D507C900956679 /* CopyFiles */ = {
18+
isa = PBXCopyFilesBuildPhase;
19+
buildActionMask = 2147483647;
20+
dstPath = /usr/share/man/man1/;
21+
dstSubfolderSpec = 0;
22+
files = (
23+
);
24+
runOnlyForDeploymentPostprocessing = 1;
25+
};
26+
/* End PBXCopyFilesBuildPhase section */
27+
28+
/* Begin PBXFileReference section */
29+
DA7BA25521D507C900956679 /* composite */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = composite; sourceTree = BUILT_PRODUCTS_DIR; };
30+
DA7BA25821D507C900956679 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
31+
DA7BA26021D5081C00956679 /* Company.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Company.h; sourceTree = "<group>"; };
32+
DA7BA26121D508E200956679 /* ConcreteCompany.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ConcreteCompany.h; sourceTree = "<group>"; };
33+
DA7BA26221D508E200956679 /* ConcreteCompany.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ConcreteCompany.m; sourceTree = "<group>"; };
34+
DA7BA26421D5090900956679 /* HRDepartment.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HRDepartment.h; sourceTree = "<group>"; };
35+
DA7BA26521D5090900956679 /* HRDepartment.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HRDepartment.m; sourceTree = "<group>"; };
36+
DA7BA26721D5092200956679 /* FinanceDepartment.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FinanceDepartment.h; sourceTree = "<group>"; };
37+
DA7BA26821D5092200956679 /* FinanceDepartment.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FinanceDepartment.m; sourceTree = "<group>"; };
38+
/* End PBXFileReference section */
39+
40+
/* Begin PBXFrameworksBuildPhase section */
41+
DA7BA25221D507C900956679 /* Frameworks */ = {
42+
isa = PBXFrameworksBuildPhase;
43+
buildActionMask = 2147483647;
44+
files = (
45+
);
46+
runOnlyForDeploymentPostprocessing = 0;
47+
};
48+
/* End PBXFrameworksBuildPhase section */
49+
50+
/* Begin PBXGroup section */
51+
DA7BA24C21D507C900956679 = {
52+
isa = PBXGroup;
53+
children = (
54+
DA7BA25721D507C900956679 /* composite */,
55+
DA7BA25621D507C900956679 /* Products */,
56+
);
57+
sourceTree = "<group>";
58+
};
59+
DA7BA25621D507C900956679 /* Products */ = {
60+
isa = PBXGroup;
61+
children = (
62+
DA7BA25521D507C900956679 /* composite */,
63+
);
64+
name = Products;
65+
sourceTree = "<group>";
66+
};
67+
DA7BA25721D507C900956679 /* composite */ = {
68+
isa = PBXGroup;
69+
children = (
70+
DA7BA25F21D507E000956679 /* composite */,
71+
DA7BA25821D507C900956679 /* main.m */,
72+
);
73+
path = composite;
74+
sourceTree = "<group>";
75+
};
76+
DA7BA25F21D507E000956679 /* composite */ = {
77+
isa = PBXGroup;
78+
children = (
79+
DA7BA26021D5081C00956679 /* Company.h */,
80+
DA7BA26121D508E200956679 /* ConcreteCompany.h */,
81+
DA7BA26221D508E200956679 /* ConcreteCompany.m */,
82+
DA7BA26421D5090900956679 /* HRDepartment.h */,
83+
DA7BA26521D5090900956679 /* HRDepartment.m */,
84+
DA7BA26721D5092200956679 /* FinanceDepartment.h */,
85+
DA7BA26821D5092200956679 /* FinanceDepartment.m */,
86+
);
87+
path = composite;
88+
sourceTree = "<group>";
89+
};
90+
/* End PBXGroup section */
91+
92+
/* Begin PBXNativeTarget section */
93+
DA7BA25421D507C900956679 /* composite */ = {
94+
isa = PBXNativeTarget;
95+
buildConfigurationList = DA7BA25C21D507C900956679 /* Build configuration list for PBXNativeTarget "composite" */;
96+
buildPhases = (
97+
DA7BA25121D507C900956679 /* Sources */,
98+
DA7BA25221D507C900956679 /* Frameworks */,
99+
DA7BA25321D507C900956679 /* CopyFiles */,
100+
);
101+
buildRules = (
102+
);
103+
dependencies = (
104+
);
105+
name = composite;
106+
productName = composite;
107+
productReference = DA7BA25521D507C900956679 /* composite */;
108+
productType = "com.apple.product-type.tool";
109+
};
110+
/* End PBXNativeTarget section */
111+
112+
/* Begin PBXProject section */
113+
DA7BA24D21D507C900956679 /* Project object */ = {
114+
isa = PBXProject;
115+
attributes = {
116+
LastUpgradeCheck = 1010;
117+
ORGANIZATIONNAME = "王俊";
118+
TargetAttributes = {
119+
DA7BA25421D507C900956679 = {
120+
CreatedOnToolsVersion = 10.1;
121+
};
122+
};
123+
};
124+
buildConfigurationList = DA7BA25021D507C900956679 /* Build configuration list for PBXProject "composite" */;
125+
compatibilityVersion = "Xcode 9.3";
126+
developmentRegion = en;
127+
hasScannedForEncodings = 0;
128+
knownRegions = (
129+
en,
130+
);
131+
mainGroup = DA7BA24C21D507C900956679;
132+
productRefGroup = DA7BA25621D507C900956679 /* Products */;
133+
projectDirPath = "";
134+
projectRoot = "";
135+
targets = (
136+
DA7BA25421D507C900956679 /* composite */,
137+
);
138+
};
139+
/* End PBXProject section */
140+
141+
/* Begin PBXSourcesBuildPhase section */
142+
DA7BA25121D507C900956679 /* Sources */ = {
143+
isa = PBXSourcesBuildPhase;
144+
buildActionMask = 2147483647;
145+
files = (
146+
DA7BA26621D5090900956679 /* HRDepartment.m in Sources */,
147+
DA7BA26321D508E200956679 /* ConcreteCompany.m in Sources */,
148+
DA7BA26921D5092200956679 /* FinanceDepartment.m in Sources */,
149+
DA7BA25921D507C900956679 /* main.m in Sources */,
150+
);
151+
runOnlyForDeploymentPostprocessing = 0;
152+
};
153+
/* End PBXSourcesBuildPhase section */
154+
155+
/* Begin XCBuildConfiguration section */
156+
DA7BA25A21D507C900956679 /* Debug */ = {
157+
isa = XCBuildConfiguration;
158+
buildSettings = {
159+
ALWAYS_SEARCH_USER_PATHS = NO;
160+
CLANG_ANALYZER_NONNULL = YES;
161+
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
162+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
163+
CLANG_CXX_LIBRARY = "libc++";
164+
CLANG_ENABLE_MODULES = YES;
165+
CLANG_ENABLE_OBJC_ARC = YES;
166+
CLANG_ENABLE_OBJC_WEAK = YES;
167+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
168+
CLANG_WARN_BOOL_CONVERSION = YES;
169+
CLANG_WARN_COMMA = YES;
170+
CLANG_WARN_CONSTANT_CONVERSION = YES;
171+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
172+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
173+
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
174+
CLANG_WARN_EMPTY_BODY = YES;
175+
CLANG_WARN_ENUM_CONVERSION = YES;
176+
CLANG_WARN_INFINITE_RECURSION = YES;
177+
CLANG_WARN_INT_CONVERSION = YES;
178+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
179+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
180+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
181+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
182+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
183+
CLANG_WARN_STRICT_PROTOTYPES = YES;
184+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
185+
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
186+
CLANG_WARN_UNREACHABLE_CODE = YES;
187+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
188+
CODE_SIGN_IDENTITY = "Mac Developer";
189+
COPY_PHASE_STRIP = NO;
190+
DEBUG_INFORMATION_FORMAT = dwarf;
191+
ENABLE_STRICT_OBJC_MSGSEND = YES;
192+
ENABLE_TESTABILITY = YES;
193+
GCC_C_LANGUAGE_STANDARD = gnu11;
194+
GCC_DYNAMIC_NO_PIC = NO;
195+
GCC_NO_COMMON_BLOCKS = YES;
196+
GCC_OPTIMIZATION_LEVEL = 0;
197+
GCC_PREPROCESSOR_DEFINITIONS = (
198+
"DEBUG=1",
199+
"$(inherited)",
200+
);
201+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
202+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
203+
GCC_WARN_UNDECLARED_SELECTOR = YES;
204+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
205+
GCC_WARN_UNUSED_FUNCTION = YES;
206+
GCC_WARN_UNUSED_VARIABLE = YES;
207+
MACOSX_DEPLOYMENT_TARGET = 10.14;
208+
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
209+
MTL_FAST_MATH = YES;
210+
ONLY_ACTIVE_ARCH = YES;
211+
SDKROOT = macosx;
212+
};
213+
name = Debug;
214+
};
215+
DA7BA25B21D507C900956679 /* Release */ = {
216+
isa = XCBuildConfiguration;
217+
buildSettings = {
218+
ALWAYS_SEARCH_USER_PATHS = NO;
219+
CLANG_ANALYZER_NONNULL = YES;
220+
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
221+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
222+
CLANG_CXX_LIBRARY = "libc++";
223+
CLANG_ENABLE_MODULES = YES;
224+
CLANG_ENABLE_OBJC_ARC = YES;
225+
CLANG_ENABLE_OBJC_WEAK = YES;
226+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
227+
CLANG_WARN_BOOL_CONVERSION = YES;
228+
CLANG_WARN_COMMA = YES;
229+
CLANG_WARN_CONSTANT_CONVERSION = YES;
230+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
231+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
232+
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
233+
CLANG_WARN_EMPTY_BODY = YES;
234+
CLANG_WARN_ENUM_CONVERSION = YES;
235+
CLANG_WARN_INFINITE_RECURSION = YES;
236+
CLANG_WARN_INT_CONVERSION = YES;
237+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
238+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
239+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
240+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
241+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
242+
CLANG_WARN_STRICT_PROTOTYPES = YES;
243+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
244+
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
245+
CLANG_WARN_UNREACHABLE_CODE = YES;
246+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
247+
CODE_SIGN_IDENTITY = "Mac Developer";
248+
COPY_PHASE_STRIP = NO;
249+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
250+
ENABLE_NS_ASSERTIONS = NO;
251+
ENABLE_STRICT_OBJC_MSGSEND = YES;
252+
GCC_C_LANGUAGE_STANDARD = gnu11;
253+
GCC_NO_COMMON_BLOCKS = YES;
254+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
255+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
256+
GCC_WARN_UNDECLARED_SELECTOR = YES;
257+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
258+
GCC_WARN_UNUSED_FUNCTION = YES;
259+
GCC_WARN_UNUSED_VARIABLE = YES;
260+
MACOSX_DEPLOYMENT_TARGET = 10.14;
261+
MTL_ENABLE_DEBUG_INFO = NO;
262+
MTL_FAST_MATH = YES;
263+
SDKROOT = macosx;
264+
};
265+
name = Release;
266+
};
267+
DA7BA25D21D507C900956679 /* Debug */ = {
268+
isa = XCBuildConfiguration;
269+
buildSettings = {
270+
CODE_SIGN_STYLE = Automatic;
271+
DEVELOPMENT_TEAM = 7465LVKW35;
272+
PRODUCT_NAME = "$(TARGET_NAME)";
273+
};
274+
name = Debug;
275+
};
276+
DA7BA25E21D507C900956679 /* Release */ = {
277+
isa = XCBuildConfiguration;
278+
buildSettings = {
279+
CODE_SIGN_STYLE = Automatic;
280+
DEVELOPMENT_TEAM = 7465LVKW35;
281+
PRODUCT_NAME = "$(TARGET_NAME)";
282+
};
283+
name = Release;
284+
};
285+
/* End XCBuildConfiguration section */
286+
287+
/* Begin XCConfigurationList section */
288+
DA7BA25021D507C900956679 /* Build configuration list for PBXProject "composite" */ = {
289+
isa = XCConfigurationList;
290+
buildConfigurations = (
291+
DA7BA25A21D507C900956679 /* Debug */,
292+
DA7BA25B21D507C900956679 /* Release */,
293+
);
294+
defaultConfigurationIsVisible = 0;
295+
defaultConfigurationName = Release;
296+
};
297+
DA7BA25C21D507C900956679 /* Build configuration list for PBXNativeTarget "composite" */ = {
298+
isa = XCConfigurationList;
299+
buildConfigurations = (
300+
DA7BA25D21D507C900956679 /* Debug */,
301+
DA7BA25E21D507C900956679 /* Release */,
302+
);
303+
defaultConfigurationIsVisible = 0;
304+
defaultConfigurationName = Release;
305+
};
306+
/* End XCConfigurationList section */
307+
};
308+
rootObject = DA7BA24D21D507C900956679 /* Project object */;
309+
}

da-hua-she-ji-mo-shi/19-composite/composite/composite.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Company.h
3+
// composite
4+
//
5+
// Created by 王俊 on 2018/12/27.
6+
// Copyright © 2018 王俊. All rights reserved.
7+
//
8+
9+
#ifndef Company_h
10+
#define Company_h
11+
12+
@protocol Company <NSObject>
13+
14+
@required
15+
16+
@property (nonatomic, copy) NSString *name;
17+
18+
- (id<Company>)initWithName:(NSString *)name;
19+
- (void)addCompany:(id<Company>)company;
20+
- (void)removeCompany:(id<Company>)company;
21+
- (void)display:(NSInteger)depth;
22+
- (void)lineOfDuty;
23+
24+
@end
25+
26+
#endif /* Company_h */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ConcreteCompany.h
3+
// composite
4+
//
5+
// Created by 王俊 on 2018/12/27.
6+
// Copyright © 2018 王俊. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "Company.h"
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
@interface ConcreteCompany : NSObject<Company>
15+
16+
@end
17+
18+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)