delete unused files and delete copy ctor, copy assign for MementoPattern

master
FengJungle 2021-10-30 10:45:26 +08:00
parent 0ecf749949
commit cbfbfda1e8
4 changed files with 30 additions and 60 deletions

View File

@ -3,26 +3,38 @@
#include "Memento.h"
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
// 管理者
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class CodeManager
{
public:
CodeManager(){}
~CodeManager(){
for(auto* memento: mementoList){
if(memento){
delete memento;
memento = nullptr;
}
}
}
CodeManager(const CodeManager&) = delete;
CodeManager& operator=(const CodeManager&) = delete;
void commit(Memento* m){
printf("提交:版本-%d, 日期-%s, 标签-%s\n", m->getVersion(), m->getDate().c_str(), m->getLabel().c_str());
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>汾-%d, <20><><EFBFBD><EFBFBD>-%s, <20><>ǩ-%s\n", m->getVersion(), m->getDate().c_str(), m->getLabel().c_str());
mementoList.push_back(m);
}
// 切换到指定的版本,即回退到指定版本
// <EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>İ汾<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>
Memento* switchToPointedVersion(int index){
mementoList.erase(mementoList.begin() + mementoList.size() - index, mementoList.end());
return mementoList[mementoList.size() - 1];
}
// 打印历史版本
// <EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD>ʷ<EFBFBD>
void codeLog(){
for (int i = 0; i < mementoList.size(); i++){
printf("[%d]:版本-%d, 日期-%s, 标签-%s\n", i, mementoList[i]->getVersion(),
printf("[%d]<EFBFBD><EFBFBD><EFBFBD>汾-%d, <20><><EFBFBD><EFBFBD>-%s, <20><>ǩ-%s\n", i, mementoList[i]->getVersion(),
mementoList[i]->getDate().c_str(), mementoList[i]->getLabel().c_str());
}
}

View File

@ -1,6 +1,9 @@
#ifndef __MEMENTO_H__
#define __MEMENTO_H__
#include <string>
using namespace std;
class Memento
{
public:

View File

@ -1,49 +0,0 @@
#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

@ -6,7 +6,7 @@ using namespace std;
#include "Memento.h"
// 原生器:CodeVersion
// ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CodeVersion
class CodeVersion
{
public:
@ -20,11 +20,15 @@ public:
date = iDate;
label = iLabel;
}
// 保存代码
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Memento* save(){
return new Memento(this->version, this->date, this->label);
}
// 回退版本
Memento *commit()
{
return new Memento(this->version, this->date, this->label);
}
// <20><><EFBFBD>˰汾
void restore(Memento* memento){
setVersion(memento->getVersion());
setDate(memento->getDate());
@ -49,11 +53,11 @@ public:
return date;
}
private:
// 代码版本
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int version;
// 代码提交日期
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
string date;
// 代码标签
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ
string label;
};