From cbfbfda1e8b9cb1253f1bd43da972aa2e157dd73 Mon Sep 17 00:00:00 2001 From: FengJungle Date: Sat, 30 Oct 2021 10:45:26 +0800 Subject: [PATCH] delete unused files and delete copy ctor, copy assign for MementoPattern --- 19.MementoPattern/2.Code/CodeManager.h | 22 ++++++++--- 19.MementoPattern/2.Code/Memento.h | 3 ++ 19.MementoPattern/2.Code/Originator.cpp | 49 ------------------------- 19.MementoPattern/2.Code/Originator.h | 16 +++++--- 4 files changed, 30 insertions(+), 60 deletions(-) delete mode 100644 19.MementoPattern/2.Code/Originator.cpp diff --git a/19.MementoPattern/2.Code/CodeManager.h b/19.MementoPattern/2.Code/CodeManager.h index a5d461e..80de8c3 100644 --- a/19.MementoPattern/2.Code/CodeManager.h +++ b/19.MementoPattern/2.Code/CodeManager.h @@ -3,26 +3,38 @@ #include "Memento.h" #include +#include +#include using namespace std; -// ¹ÜÀíÕß +// ������ 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("�ύ���汾-%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(), + printf("[%d]���汾-%d, ����-%s, ��ǩ-%s\n", i, mementoList[i]->getVersion(), mementoList[i]->getDate().c_str(), mementoList[i]->getLabel().c_str()); } } diff --git a/19.MementoPattern/2.Code/Memento.h b/19.MementoPattern/2.Code/Memento.h index 3ae9146..38e4324 100644 --- a/19.MementoPattern/2.Code/Memento.h +++ b/19.MementoPattern/2.Code/Memento.h @@ -1,6 +1,9 @@ #ifndef __MEMENTO_H__ #define __MEMENTO_H__ +#include +using namespace std; + class Memento { public: diff --git a/19.MementoPattern/2.Code/Originator.cpp b/19.MementoPattern/2.Code/Originator.cpp deleted file mode 100644 index c4edc66..0000000 --- a/19.MementoPattern/2.Code/Originator.cpp +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/19.MementoPattern/2.Code/Originator.h b/19.MementoPattern/2.Code/Originator.h index 1a9b15d..9f5bcf6 100644 --- a/19.MementoPattern/2.Code/Originator.h +++ b/19.MementoPattern/2.Code/Originator.h @@ -6,7 +6,7 @@ using namespace std; #include "Memento.h" -// Ô­ÉúÆ÷£ºCodeVersion +// ԭ������CodeVersion class CodeVersion { public: @@ -20,11 +20,15 @@ public: date = iDate; label = iLabel; } - // ±£´æ´úÂë + // ������� Memento* save(){ return new Memento(this->version, this->date, this->label); } - // »ØÍË°æ±¾ + Memento *commit() + { + return new Memento(this->version, this->date, this->label); + } + // ���˰汾 void restore(Memento* memento){ setVersion(memento->getVersion()); setDate(memento->getDate()); @@ -49,11 +53,11 @@ public: return date; } private: - // ´úÂë°æ±¾ + // ����汾 int version; - // ´úÂëÌá½»ÈÕÆÚ + // �����ύ���� string date; - // ´úÂë±êÇ© + // �����ǩ string label; };