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 "Memento.h"
#include <vector> #include <vector>
#include <string>
#include <stdio.h>
using namespace std; using namespace std;
// 管理者 // <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class CodeManager class CodeManager
{ {
public: public:
CodeManager(){} 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){ 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); 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){ Memento* switchToPointedVersion(int index){
mementoList.erase(mementoList.begin() + mementoList.size() - index, mementoList.end()); mementoList.erase(mementoList.begin() + mementoList.size() - index, mementoList.end());
return mementoList[mementoList.size() - 1]; return mementoList[mementoList.size() - 1];
} }
// 打印历史版本 // <EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD>ʷ<EFBFBD>
void codeLog(){ void codeLog(){
for (int i = 0; i < mementoList.size(); i++){ 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()); mementoList[i]->getDate().c_str(), mementoList[i]->getLabel().c_str());
} }
} }

View File

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