From 66ff26ae24a39ef77a0572ff4f7a38bac6a6e1ed Mon Sep 17 00:00:00 2001 From: Micooz Date: Sun, 5 Oct 2014 13:08:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=A4=87=E5=BF=98=E5=BD=95?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DesignPattern.sln | 6 ++ DesignPattern.v12.suo | Bin 56320 -> 67584 bytes MementoPattern/MementoPattern.cpp | 79 ++++++++++++++++ MementoPattern/MementoPattern.vcxproj | 84 ++++++++++++++++++ MementoPattern/MementoPattern.vcxproj.filters | 22 +++++ 5 files changed, 191 insertions(+) create mode 100644 MementoPattern/MementoPattern.cpp create mode 100644 MementoPattern/MementoPattern.vcxproj create mode 100644 MementoPattern/MementoPattern.vcxproj.filters diff --git a/DesignPattern.sln b/DesignPattern.sln index e8fd94f..71bcff5 100644 --- a/DesignPattern.sln +++ b/DesignPattern.sln @@ -35,6 +35,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StatePattern", "StatePatter EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ObserverPattern", "ObserverPattern\ObserverPattern.vcxproj", "{052C8FBE-6E06-4869-B779-B5AF6D5AEC65}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MementoPattern", "MementoPattern\MementoPattern.vcxproj", "{9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -105,6 +107,10 @@ Global {052C8FBE-6E06-4869-B779-B5AF6D5AEC65}.Debug|Win32.Build.0 = Debug|Win32 {052C8FBE-6E06-4869-B779-B5AF6D5AEC65}.Release|Win32.ActiveCfg = Release|Win32 {052C8FBE-6E06-4869-B779-B5AF6D5AEC65}.Release|Win32.Build.0 = Release|Win32 + {9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Debug|Win32.ActiveCfg = Debug|Win32 + {9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Debug|Win32.Build.0 = Debug|Win32 + {9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Release|Win32.ActiveCfg = Release|Win32 + {9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DesignPattern.v12.suo b/DesignPattern.v12.suo index d2522b0ec34b1af975335f59861f9713e3b31283..b32d7b969273f0f4dccbc3ff059e1347f7048225 100644 GIT binary patch delta 6779 zcmeHMe^AuN760yW2X}yYfFJ_OiHL}R_XAE4m4gHPg&b-$sUZqQqNfN14*V^rvFaqI zm3yg4(@Dm1#3qdymGf7Kv1ie$O`>`WY7}_2gPY6X|yCsLBQv|y}hU;0XS*D%C^6%O1Knt z5f#&(Dp+vgoXF;eB3h%Et`X?lI}$uO?P=oQbVXrP$AVS+Mc1e$sBO@{!18ch;z=l@ zsY(@R&jQE4AL6Td;=!PY0to=~8+~m$)X~5oA0LG}#YgiPT*AO7`D6yv8i17cyB-_IcFX|pWjn4>gbKQLq_Np{jXjM4# zPvDn72dFMIG29KJ6L^LNkA_yn9Ru+qa1?k6;9vBhrt6_P92hQRolxji@1UifR!7ZLbHH-&$^9-z*)&ocWjv0WH|d{zwHC4w zpleVsQ$P-T4gn2+$VJv7@dSFX_ql`&ZQqugx9H^Gx(pv)X?|g`XNN9VD09)m3LD=5 z@f`O4(b`pWs%N_%}xUCZLy%4SECy?zNd2T!VRsjHhpqO*W53`-!((_Yec z>fsu#XvJEx$IX&z;l5g_9kRdCq{^~L8$ax_L$lIfx=b=u9viqGT~u}g2R2uC4Sgr$yoxGT{g zr^9!VE-K{?p~t~?EA;Zf4Gg{TTcL-p8%-vUDXUv(%L?B+)^R=c#l7lb-5cE*y>w&z zR5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uE@ diff --git a/MementoPattern/MementoPattern.cpp b/MementoPattern/MementoPattern.cpp new file mode 100644 index 0000000..0080e28 --- /dev/null +++ b/MementoPattern/MementoPattern.cpp @@ -0,0 +1,79 @@ +#include +#include +using namespace std; + +class Memento { +private: + friend class Originator; + + Memento(const string& st) { + _st = st; + } + + void SetState(const string& st) { + _st = st; + } + + string GetState() { + return _st; + } + +private: + string _st; +}; + +class Originator { +public: + Originator() { + _mt = nullptr; + } + + Originator(const string &st) { + _st = st; + _mt = nullptr; + } + + Memento* CreateMemento() { + return new Memento(_st); + } + + void SetMemento(Memento* mt) { + _mt = mt; + } + + void RestoreToMemento(Memento* mt) { + _st = mt->GetState(); + } + + string GetState() { + return _st; + } + + void SetState(const string& st) { + _st = st; + } + + void PrintState() { + cout << _st << "..." << endl; + } + +private: + string _st; + Memento *_mt; +}; + +int main() { + Originator *o = new Originator(); + o->SetState("old"); + o->PrintState(); + Memento *m = o->CreateMemento(); + o->SetState("new"); + o->PrintState(); + o->RestoreToMemento(m); + o->PrintState(); + + delete o; + delete m; + + return 0; +} diff --git a/MementoPattern/MementoPattern.vcxproj b/MementoPattern/MementoPattern.vcxproj new file mode 100644 index 0000000..21f1d4e --- /dev/null +++ b/MementoPattern/MementoPattern.vcxproj @@ -0,0 +1,84 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {9727E0D3-66C5-4812-B13C-DC4C1F06ACAD} + Win32Proj + MementoPattern + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/MementoPattern/MementoPattern.vcxproj.filters b/MementoPattern/MementoPattern.vcxproj.filters new file mode 100644 index 0000000..53b5a43 --- /dev/null +++ b/MementoPattern/MementoPattern.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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