diff --git a/19.MementoPattern/1.Picture/备忘录模式.jpg b/19.MementoPattern/1.Picture/备忘录模式.jpg new file mode 100644 index 0000000..72b16a0 Binary files /dev/null and b/19.MementoPattern/1.Picture/备忘录模式.jpg differ diff --git a/19.MementoPattern/1.Picture/备忘录模式UML图.png b/19.MementoPattern/1.Picture/备忘录模式UML图.png new file mode 100644 index 0000000..518eb7b Binary files /dev/null and b/19.MementoPattern/1.Picture/备忘录模式UML图.png differ diff --git a/19.MementoPattern/1.Picture/备忘录模式实例UML图.png b/19.MementoPattern/1.Picture/备忘录模式实例UML图.png new file mode 100644 index 0000000..00667d5 Binary files /dev/null and b/19.MementoPattern/1.Picture/备忘录模式实例UML图.png differ diff --git a/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.exe b/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.exe new file mode 100644 index 0000000..bda192a Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.exe differ diff --git a/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.ilk b/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.ilk new file mode 100644 index 0000000..9ecc00d Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.ilk differ diff --git a/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.pdb b/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.pdb new file mode 100644 index 0000000..14b1c80 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/Debug/MementoPattern.pdb differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern.sdf b/19.MementoPattern/2.Code/MementoPattern/MementoPattern.sdf new file mode 100644 index 0000000..51d65fe Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern.sdf differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern.sln b/19.MementoPattern/2.Code/MementoPattern/MementoPattern.sln new file mode 100644 index 0000000..08db605 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MementoPattern", "MementoPattern\MementoPattern.vcxproj", "{F79EC45A-55BA-4A7A-8EBF-22B1F644E689}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F79EC45A-55BA-4A7A-8EBF-22B1F644E689}.Debug|Win32.ActiveCfg = Debug|Win32 + {F79EC45A-55BA-4A7A-8EBF-22B1F644E689}.Debug|Win32.Build.0 = Debug|Win32 + {F79EC45A-55BA-4A7A-8EBF-22B1F644E689}.Release|Win32.ActiveCfg = Release|Win32 + {F79EC45A-55BA-4A7A-8EBF-22B1F644E689}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern.v12.suo b/19.MementoPattern/2.Code/MementoPattern/MementoPattern.v12.suo new file mode 100644 index 0000000..1461a45 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern.v12.suo differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/CodeManager.h b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/CodeManager.h new file mode 100644 index 0000000..a5d461e --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/CodeManager.h @@ -0,0 +1,33 @@ +#ifndef __CODEMANAGER_H__ +#define __CODEMANAGER_H__ + +#include "Memento.h" +#include +using namespace std; + +// +class CodeManager +{ +public: + CodeManager(){} + void commit(Memento* m){ + printf("ύ汾-%d, -%s, ǩ-%s\n", m->getVersion(), m->getDate().c_str(), m->getLabel().c_str()); + mementoList.push_back(m); + } + // лָİ汾˵ָ汾 + Memento* switchToPointedVersion(int index){ + mementoList.erase(mementoList.begin() + mementoList.size() - index, mementoList.end()); + return mementoList[mementoList.size() - 1]; + } + // ӡʷ汾 + void codeLog(){ + for (int i = 0; i < mementoList.size(); i++){ + printf("[%d]汾-%d, -%s, ǩ-%s\n", i, mementoList[i]->getVersion(), + mementoList[i]->getDate().c_str(), mementoList[i]->getLabel().c_str()); + } + } +private: + vector mementoList; +}; + +#endif \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.log b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.log new file mode 100644 index 0000000..8a42bd8 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.log @@ -0,0 +1,14 @@ +生成启动时间为 2019/11/5 22:43:13。 + 1>项目“G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\MementoPattern\MementoPattern.vcxproj”在节点 2 上(Build 个目标)。 + 1>ClCompile: + E:\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt main.cpp + main.cpp + 1>g:\8.new_cd\3.study\8.design pattern\19.mementopattern\2.code\mementopattern\mementopattern\codemanager.h(24): warning C4018: “<”: 有符号/无符号不匹配 + Link: + E:\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\Debug\MementoPattern.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\Debug\MementoPattern.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\Debug\MementoPattern.lib" /MACHINE:X86 Debug\main.obj + MementoPattern.vcxproj -> G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\Debug\MementoPattern.exe + 1>已完成生成项目“G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\MementoPattern\MementoPattern.vcxproj”(Build 个目标)的操作。 + +生成成功。 + +已用时间 00:00:01.85 diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/CL.read.1.tlog b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/CL.read.1.tlog new file mode 100644 index 0000000..9738d67 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/CL.read.1.tlog differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/CL.write.1.tlog b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/CL.write.1.tlog new file mode 100644 index 0000000..35164de Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/CL.write.1.tlog differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/MementoPattern.lastbuildstate b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/MementoPattern.lastbuildstate new file mode 100644 index 0000000..71782b7 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/MementoPattern.lastbuildstate @@ -0,0 +1,2 @@ +#TargetFrameworkVersion=v4.0:PlatformToolSet=v120:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit +Debug|Win32|G:\8.New_cd\3.Study\8.Design Pattern\19.MementoPattern\2.Code\MementoPattern\| diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/cl.command.1.tlog b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/cl.command.1.tlog new file mode 100644 index 0000000..d0072b3 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/cl.command.1.tlog differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.command.1.tlog b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.command.1.tlog new file mode 100644 index 0000000..9b98db0 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.command.1.tlog differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.read.1.tlog b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.read.1.tlog new file mode 100644 index 0000000..d4bae3d Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.read.1.tlog differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.write.1.tlog b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.write.1.tlog new file mode 100644 index 0000000..ebb9de3 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/MementoPattern.tlog/link.write.1.tlog differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/main.obj b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/main.obj new file mode 100644 index 0000000..eade70d Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/main.obj differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/vc120.idb b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/vc120.idb new file mode 100644 index 0000000..a29d4f4 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/vc120.idb differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/vc120.pdb b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/vc120.pdb new file mode 100644 index 0000000..91c2152 Binary files /dev/null and b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Debug/vc120.pdb differ diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Memento.h b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Memento.h new file mode 100644 index 0000000..3ae9146 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Memento.h @@ -0,0 +1,37 @@ +#ifndef __MEMENTO_H__ +#define __MEMENTO_H__ + +class Memento +{ +public: + Memento(){} + Memento(int iVersion, string iDate, string iLabel){ + version = iVersion; + date = iDate; + label = iLabel; + } + void setVersion(int iVersion){ + version = iVersion; + } + int getVersion(){ + return version; + } + void setLabel(string iLabel){ + label = iLabel; + } + string getLabel(){ + return label; + } + void setDate(string iDate){ + date = iDate; + } + string getDate(){ + return date; + } +private: + int version; + string date; + string label; +}; + +#endif \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/MementoPattern.vcxproj b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/MementoPattern.vcxproj new file mode 100644 index 0000000..5e283c6 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/MementoPattern.vcxproj @@ -0,0 +1,92 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {F79EC45A-55BA-4A7A-8EBF-22B1F644E689} + Win32Proj + MementoPattern + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/MementoPattern.vcxproj.filters b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/MementoPattern.vcxproj.filters new file mode 100644 index 0000000..0e48db7 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/MementoPattern.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Originator.cpp b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Originator.cpp new file mode 100644 index 0000000..c4edc66 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Originator.cpp @@ -0,0 +1,49 @@ +#include "Originator.h" + +CodeVersion::CodeVersion(){ + version = 0; + date = "1900-01-01"; + label = "none"; +} + +CodeVersion::CodeVersion(int iVersion, string iDate, string iLabel) +{ + version = iVersion; + date = iDate; + label = iLabel; +} + +Memento* CodeVersion::commit(){ + return new Memento(this->version, this->date, this->label); +} + + +void CodeVersion::restore(Memento* memento){ + setVersion(memento->getVersion()); + setDate(memento->getDate()); + setLabel(memento->getLabel()); +} + +void CodeVersion::setVersion(int iVersion){ + version = iVersion; +} + +int CodeVersion::getVersion(){ + return version; +} + +void CodeVersion::setLabel(string iLabel){ + label = iLabel; +} + +string CodeVersion::getLabel(){ + return label; +} + +void CodeVersion::setDate(string iDate){ + date = iDate; +} + +string CodeVersion::getDate(){ + return date; +} \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Originator.h b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Originator.h new file mode 100644 index 0000000..1a9b15d --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/Originator.h @@ -0,0 +1,60 @@ +#ifndef __CODEVERSION_H__ +#define __CODEVERSION_H__ + +#include +using namespace std; + +#include "Memento.h" + +// ԭCodeVersion +class CodeVersion +{ +public: + CodeVersion(){ + version = 0; + date = "1900-01-01"; + label = "none"; + } + CodeVersion(int iVersion, string iDate, string iLabel){ + version = iVersion; + date = iDate; + label = iLabel; + } + // + Memento* save(){ + return new Memento(this->version, this->date, this->label); + } + // ˰汾 + void restore(Memento* memento){ + setVersion(memento->getVersion()); + setDate(memento->getDate()); + setLabel(memento->getLabel()); + } + void setVersion(int iVersion){ + version = iVersion; + } + int getVersion(){ + return version; + } + void setLabel(string iLabel){ + label = iLabel; + } + string getLabel(){ + return label; + } + void setDate(string iDate){ + date = iDate; + } + string getDate(){ + return date; + } +private: + // 汾 + int version; + // ύ + string date; + // ǩ + string label; +}; + +#endif \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/demo.h b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/demo.h new file mode 100644 index 0000000..8d19694 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/demo.h @@ -0,0 +1,90 @@ +#ifndef __DEMO_H__ +#define __DEMO_H__ + +#ifndef __DEMO_H__ +// ǰ +class Memento; + +// ԭ ʵ +class Originator +{ +public: + Originator(){ + state = ""; + } + Originator(String iState){ + state = iState; + } + // ¼ + Memento* createMemento(){ + return new Memento(this); + } + // ñ¼ָԭ״̬ + void restoreMemento(Memento* m){ + state = m->getState(); + } + void setState(string iState){ + state = iState; + } + string getState(){ + return state; + } +private: + string state; +}; + +// ¼ ʵ֣ԭƣ +class Memento +{ +public: + Memento(){ + state = ""; + } + Memento(Originator* o){ + state = o->getState(); + } + void setState(String iState){ + state = iState; + } + string getState(){ + return state; + } +private: + String state; +}; + +// ʵ +class Caretaker +{ +public: + Caretaker(){} + Memento* getMemento(){ + return memento; + } + void setMemento(Memento *m){ + memento = m; + } +private: + Memento* memento; +}; + +// ͻ ʾ +int main() +{ + // ԭ + Originator o = new Originator("״̬1"); + // ˶ + Caretaker *c = new Caretaker(); + c->setMemento(o->createMemento()); + + o->setState("״̬2"); + + // Ӹ˶ȡ¼ʵֳ + o->restoreMemento(c->getMemento()); + + return 0; +} + +#endif + +#endif // __DEMO_H__ \ No newline at end of file diff --git a/19.MementoPattern/2.Code/MementoPattern/MementoPattern/main.cpp b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/main.cpp new file mode 100644 index 0000000..c3e39f4 --- /dev/null +++ b/19.MementoPattern/2.Code/MementoPattern/MementoPattern/main.cpp @@ -0,0 +1,44 @@ +#include "Originator.h" +#include "Memento.h" +#include "CodeManager.h" + +int main() +{ + CodeManager *Jungle = new CodeManager(); + + CodeVersion* codeVer = new CodeVersion(1001, "2019-11-03", "Initial version"); + + // ύʼ汾 + printf("ύʼ汾:\n"); + Jungle->commit(codeVer->save()); + + // ޸һ汾־ + printf("\nύһ汾־:\n"); + codeVer->setVersion(1002); + codeVer->setDate("2019-11-04"); + codeVer->setLabel("Add log funciton"); + Jungle->commit(codeVer->save()); + + // ޸һ汾QtͼƬ + printf("\nύһ汾QtͼƬ:\n"); + codeVer->setVersion(1003); + codeVer->setDate("2019-11-05"); + codeVer->setLabel("Add Qt Image Browser"); + Jungle->commit(codeVer->save()); + + // 鿴ύʷ + printf("\n鿴ύʷ\n"); + Jungle->codeLog(); + + // ˵һ汾 + printf("\n˵һ汾\n"); + codeVer->restore(Jungle->switchToPointedVersion(1)); + + // 鿴ύʷ + printf("\n鿴ύʷ\n"); + Jungle->codeLog(); + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index fff718c..d665246 100644 --- a/README.md +++ b/README.md @@ -85,4 +85,8 @@ Jungle设计模式系列 21.设计模式(二十一)——中介者模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102885567 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102885567 + +22.设计模式(二十二)——备忘录模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102907007 \ No newline at end of file