add Memento Pattern

master
FengJungle 2019-11-05 22:47:55 +08:00
parent ea451138ef
commit ea62a3b182
29 changed files with 484 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -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

View File

@ -0,0 +1,33 @@
#ifndef __CODEMANAGER_H__
#define __CODEMANAGER_H__
#include "Memento.h"
#include <vector>
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<Memento*> mementoList;
};
#endif

View File

@ -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

View File

@ -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\|

View File

@ -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

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{F79EC45A-55BA-4A7A-8EBF-22B1F644E689}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>MementoPattern</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="CodeManager.h" />
<ClInclude Include="demo.h" />
<ClInclude Include="Memento.h" />
<ClInclude Include="Originator.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="demo.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Originator.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Memento.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CodeManager.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -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;
}

View File

@ -0,0 +1,60 @@
#ifndef __CODEVERSION_H__
#define __CODEVERSION_H__
#include <iostream>
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

View File

@ -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__

View File

@ -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;
}

View File

@ -86,3 +86,7 @@ Jungle设计模式系列
21.设计模式(二十一)——中介者模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102885567
22.设计模式(二十二)——备忘录模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102907007