Skip to content

Commit 6fe5f84

Browse files
memento 模式
1 parent ca4d4ce commit 6fe5f84

File tree

11 files changed

+529
-0
lines changed

11 files changed

+529
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 备忘录模式
2+
3+
## 什么是备忘录模式?
4+
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复至原先保存的状态
5+
6+
7+
## 备忘录模式有哪些优点?
8+
* 解耦合,对象无需关心状态管理
9+
10+
## 备忘录模式有哪些缺点?
11+
* 状态较多时,可能会造成较大的资源消耗
12+
13+
14+
## 适用场景
15+
* 功能比较复杂,但需要维护或记录属性历史的类
16+
* 需要保存的属性,只是众多属性中的一小部分
17+
18+
19+
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 50;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
DA7BA23C21D4FF5F00956679 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA23B21D4FF5F00956679 /* main.m */; };
11+
DA7BA24521D5007B00956679 /* GameRole.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA24421D5007B00956679 /* GameRole.m */; };
12+
DA7BA24821D501BA00956679 /* RoleStateMemento.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA24721D501BA00956679 /* RoleStateMemento.m */; };
13+
DA7BA24B21D5026D00956679 /* RoleStateCaretaker.m in Sources */ = {isa = PBXBuildFile; fileRef = DA7BA24A21D5026D00956679 /* RoleStateCaretaker.m */; };
14+
/* End PBXBuildFile section */
15+
16+
/* Begin PBXCopyFilesBuildPhase section */
17+
DA7BA23621D4FF5F00956679 /* 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+
DA7BA23821D4FF5F00956679 /* memento */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = memento; sourceTree = BUILT_PRODUCTS_DIR; };
30+
DA7BA23B21D4FF5F00956679 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
31+
DA7BA24321D5007B00956679 /* GameRole.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GameRole.h; sourceTree = "<group>"; };
32+
DA7BA24421D5007B00956679 /* GameRole.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GameRole.m; sourceTree = "<group>"; };
33+
DA7BA24621D501BA00956679 /* RoleStateMemento.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RoleStateMemento.h; sourceTree = "<group>"; };
34+
DA7BA24721D501BA00956679 /* RoleStateMemento.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RoleStateMemento.m; sourceTree = "<group>"; };
35+
DA7BA24921D5026D00956679 /* RoleStateCaretaker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RoleStateCaretaker.h; sourceTree = "<group>"; };
36+
DA7BA24A21D5026D00956679 /* RoleStateCaretaker.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RoleStateCaretaker.m; sourceTree = "<group>"; };
37+
/* End PBXFileReference section */
38+
39+
/* Begin PBXFrameworksBuildPhase section */
40+
DA7BA23521D4FF5F00956679 /* Frameworks */ = {
41+
isa = PBXFrameworksBuildPhase;
42+
buildActionMask = 2147483647;
43+
files = (
44+
);
45+
runOnlyForDeploymentPostprocessing = 0;
46+
};
47+
/* End PBXFrameworksBuildPhase section */
48+
49+
/* Begin PBXGroup section */
50+
DA7BA22F21D4FF5F00956679 = {
51+
isa = PBXGroup;
52+
children = (
53+
DA7BA23A21D4FF5F00956679 /* memento */,
54+
DA7BA23921D4FF5F00956679 /* Products */,
55+
);
56+
sourceTree = "<group>";
57+
};
58+
DA7BA23921D4FF5F00956679 /* Products */ = {
59+
isa = PBXGroup;
60+
children = (
61+
DA7BA23821D4FF5F00956679 /* memento */,
62+
);
63+
name = Products;
64+
sourceTree = "<group>";
65+
};
66+
DA7BA23A21D4FF5F00956679 /* memento */ = {
67+
isa = PBXGroup;
68+
children = (
69+
DA7BA24221D4FF7100956679 /* memento */,
70+
DA7BA23B21D4FF5F00956679 /* main.m */,
71+
);
72+
path = memento;
73+
sourceTree = "<group>";
74+
};
75+
DA7BA24221D4FF7100956679 /* memento */ = {
76+
isa = PBXGroup;
77+
children = (
78+
DA7BA24321D5007B00956679 /* GameRole.h */,
79+
DA7BA24421D5007B00956679 /* GameRole.m */,
80+
DA7BA24621D501BA00956679 /* RoleStateMemento.h */,
81+
DA7BA24721D501BA00956679 /* RoleStateMemento.m */,
82+
DA7BA24921D5026D00956679 /* RoleStateCaretaker.h */,
83+
DA7BA24A21D5026D00956679 /* RoleStateCaretaker.m */,
84+
);
85+
path = memento;
86+
sourceTree = "<group>";
87+
};
88+
/* End PBXGroup section */
89+
90+
/* Begin PBXNativeTarget section */
91+
DA7BA23721D4FF5F00956679 /* memento */ = {
92+
isa = PBXNativeTarget;
93+
buildConfigurationList = DA7BA23F21D4FF5F00956679 /* Build configuration list for PBXNativeTarget "memento" */;
94+
buildPhases = (
95+
DA7BA23421D4FF5F00956679 /* Sources */,
96+
DA7BA23521D4FF5F00956679 /* Frameworks */,
97+
DA7BA23621D4FF5F00956679 /* CopyFiles */,
98+
);
99+
buildRules = (
100+
);
101+
dependencies = (
102+
);
103+
name = memento;
104+
productName = memento;
105+
productReference = DA7BA23821D4FF5F00956679 /* memento */;
106+
productType = "com.apple.product-type.tool";
107+
};
108+
/* End PBXNativeTarget section */
109+
110+
/* Begin PBXProject section */
111+
DA7BA23021D4FF5F00956679 /* Project object */ = {
112+
isa = PBXProject;
113+
attributes = {
114+
LastUpgradeCheck = 1010;
115+
ORGANIZATIONNAME = "王俊";
116+
TargetAttributes = {
117+
DA7BA23721D4FF5F00956679 = {
118+
CreatedOnToolsVersion = 10.1;
119+
};
120+
};
121+
};
122+
buildConfigurationList = DA7BA23321D4FF5F00956679 /* Build configuration list for PBXProject "memento" */;
123+
compatibilityVersion = "Xcode 9.3";
124+
developmentRegion = en;
125+
hasScannedForEncodings = 0;
126+
knownRegions = (
127+
en,
128+
);
129+
mainGroup = DA7BA22F21D4FF5F00956679;
130+
productRefGroup = DA7BA23921D4FF5F00956679 /* Products */;
131+
projectDirPath = "";
132+
projectRoot = "";
133+
targets = (
134+
DA7BA23721D4FF5F00956679 /* memento */,
135+
);
136+
};
137+
/* End PBXProject section */
138+
139+
/* Begin PBXSourcesBuildPhase section */
140+
DA7BA23421D4FF5F00956679 /* Sources */ = {
141+
isa = PBXSourcesBuildPhase;
142+
buildActionMask = 2147483647;
143+
files = (
144+
DA7BA24B21D5026D00956679 /* RoleStateCaretaker.m in Sources */,
145+
DA7BA24821D501BA00956679 /* RoleStateMemento.m in Sources */,
146+
DA7BA23C21D4FF5F00956679 /* main.m in Sources */,
147+
DA7BA24521D5007B00956679 /* GameRole.m in Sources */,
148+
);
149+
runOnlyForDeploymentPostprocessing = 0;
150+
};
151+
/* End PBXSourcesBuildPhase section */
152+
153+
/* Begin XCBuildConfiguration section */
154+
DA7BA23D21D4FF5F00956679 /* Debug */ = {
155+
isa = XCBuildConfiguration;
156+
buildSettings = {
157+
ALWAYS_SEARCH_USER_PATHS = NO;
158+
CLANG_ANALYZER_NONNULL = YES;
159+
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
160+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
161+
CLANG_CXX_LIBRARY = "libc++";
162+
CLANG_ENABLE_MODULES = YES;
163+
CLANG_ENABLE_OBJC_ARC = YES;
164+
CLANG_ENABLE_OBJC_WEAK = YES;
165+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
166+
CLANG_WARN_BOOL_CONVERSION = YES;
167+
CLANG_WARN_COMMA = YES;
168+
CLANG_WARN_CONSTANT_CONVERSION = YES;
169+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
170+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
171+
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
172+
CLANG_WARN_EMPTY_BODY = YES;
173+
CLANG_WARN_ENUM_CONVERSION = YES;
174+
CLANG_WARN_INFINITE_RECURSION = YES;
175+
CLANG_WARN_INT_CONVERSION = YES;
176+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
177+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
178+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
179+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
180+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
181+
CLANG_WARN_STRICT_PROTOTYPES = YES;
182+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
183+
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
184+
CLANG_WARN_UNREACHABLE_CODE = YES;
185+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
186+
CODE_SIGN_IDENTITY = "Mac Developer";
187+
COPY_PHASE_STRIP = NO;
188+
DEBUG_INFORMATION_FORMAT = dwarf;
189+
ENABLE_STRICT_OBJC_MSGSEND = YES;
190+
ENABLE_TESTABILITY = YES;
191+
GCC_C_LANGUAGE_STANDARD = gnu11;
192+
GCC_DYNAMIC_NO_PIC = NO;
193+
GCC_NO_COMMON_BLOCKS = YES;
194+
GCC_OPTIMIZATION_LEVEL = 0;
195+
GCC_PREPROCESSOR_DEFINITIONS = (
196+
"DEBUG=1",
197+
"$(inherited)",
198+
);
199+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
200+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
201+
GCC_WARN_UNDECLARED_SELECTOR = YES;
202+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
203+
GCC_WARN_UNUSED_FUNCTION = YES;
204+
GCC_WARN_UNUSED_VARIABLE = YES;
205+
MACOSX_DEPLOYMENT_TARGET = 10.14;
206+
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
207+
MTL_FAST_MATH = YES;
208+
ONLY_ACTIVE_ARCH = YES;
209+
SDKROOT = macosx;
210+
};
211+
name = Debug;
212+
};
213+
DA7BA23E21D4FF5F00956679 /* Release */ = {
214+
isa = XCBuildConfiguration;
215+
buildSettings = {
216+
ALWAYS_SEARCH_USER_PATHS = NO;
217+
CLANG_ANALYZER_NONNULL = YES;
218+
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
219+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
220+
CLANG_CXX_LIBRARY = "libc++";
221+
CLANG_ENABLE_MODULES = YES;
222+
CLANG_ENABLE_OBJC_ARC = YES;
223+
CLANG_ENABLE_OBJC_WEAK = YES;
224+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
225+
CLANG_WARN_BOOL_CONVERSION = YES;
226+
CLANG_WARN_COMMA = YES;
227+
CLANG_WARN_CONSTANT_CONVERSION = YES;
228+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
229+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
230+
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
231+
CLANG_WARN_EMPTY_BODY = YES;
232+
CLANG_WARN_ENUM_CONVERSION = YES;
233+
CLANG_WARN_INFINITE_RECURSION = YES;
234+
CLANG_WARN_INT_CONVERSION = YES;
235+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
236+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
237+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
238+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
239+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
240+
CLANG_WARN_STRICT_PROTOTYPES = YES;
241+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
242+
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
243+
CLANG_WARN_UNREACHABLE_CODE = YES;
244+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
245+
CODE_SIGN_IDENTITY = "Mac Developer";
246+
COPY_PHASE_STRIP = NO;
247+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
248+
ENABLE_NS_ASSERTIONS = NO;
249+
ENABLE_STRICT_OBJC_MSGSEND = YES;
250+
GCC_C_LANGUAGE_STANDARD = gnu11;
251+
GCC_NO_COMMON_BLOCKS = YES;
252+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
253+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
254+
GCC_WARN_UNDECLARED_SELECTOR = YES;
255+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
256+
GCC_WARN_UNUSED_FUNCTION = YES;
257+
GCC_WARN_UNUSED_VARIABLE = YES;
258+
MACOSX_DEPLOYMENT_TARGET = 10.14;
259+
MTL_ENABLE_DEBUG_INFO = NO;
260+
MTL_FAST_MATH = YES;
261+
SDKROOT = macosx;
262+
};
263+
name = Release;
264+
};
265+
DA7BA24021D4FF5F00956679 /* Debug */ = {
266+
isa = XCBuildConfiguration;
267+
buildSettings = {
268+
CODE_SIGN_STYLE = Automatic;
269+
DEVELOPMENT_TEAM = 7465LVKW35;
270+
PRODUCT_NAME = "$(TARGET_NAME)";
271+
};
272+
name = Debug;
273+
};
274+
DA7BA24121D4FF5F00956679 /* Release */ = {
275+
isa = XCBuildConfiguration;
276+
buildSettings = {
277+
CODE_SIGN_STYLE = Automatic;
278+
DEVELOPMENT_TEAM = 7465LVKW35;
279+
PRODUCT_NAME = "$(TARGET_NAME)";
280+
};
281+
name = Release;
282+
};
283+
/* End XCBuildConfiguration section */
284+
285+
/* Begin XCConfigurationList section */
286+
DA7BA23321D4FF5F00956679 /* Build configuration list for PBXProject "memento" */ = {
287+
isa = XCConfigurationList;
288+
buildConfigurations = (
289+
DA7BA23D21D4FF5F00956679 /* Debug */,
290+
DA7BA23E21D4FF5F00956679 /* Release */,
291+
);
292+
defaultConfigurationIsVisible = 0;
293+
defaultConfigurationName = Release;
294+
};
295+
DA7BA23F21D4FF5F00956679 /* Build configuration list for PBXNativeTarget "memento" */ = {
296+
isa = XCConfigurationList;
297+
buildConfigurations = (
298+
DA7BA24021D4FF5F00956679 /* Debug */,
299+
DA7BA24121D4FF5F00956679 /* Release */,
300+
);
301+
defaultConfigurationIsVisible = 0;
302+
defaultConfigurationName = Release;
303+
};
304+
/* End XCConfigurationList section */
305+
};
306+
rootObject = DA7BA23021D4FF5F00956679 /* Project object */;
307+
}

da-hua-she-ji-mo-shi/18-memento/memento/memento.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.
Lines changed: 8 additions & 0 deletions
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// main.m
3+
// memento
4+
//
5+
// Created by 王俊 on 2018/12/27.
6+
// Copyright © 2018 王俊. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "./memento/GameRole.h"
11+
#import "./memento/RoleStateMemento.h"
12+
#import "./memento/RoleStateCaretaker.h"
13+
14+
int main(int argc, const char * argv[]) {
15+
@autoreleasepool {
16+
// insert code here...
17+
// NSLog(@"Hello, World!");
18+
19+
// 战斗前
20+
GameRole *lixiaoyao = [GameRole new];
21+
[lixiaoyao getInitState];
22+
[lixiaoyao stateDisplay];
23+
24+
// 保存进度
25+
RoleStateCaretaker *stateAdmin = [RoleStateCaretaker new];
26+
stateAdmin.memento = [lixiaoyao saveState];
27+
28+
// 战斗后
29+
[lixiaoyao fight];
30+
[lixiaoyao stateDisplay];
31+
32+
// 恢复状态
33+
[lixiaoyao recoveryState:stateAdmin.memento];
34+
[lixiaoyao stateDisplay];
35+
36+
37+
}
38+
return 0;
39+
}

0 commit comments

Comments
 (0)