-----------------------------------------------------------------------------
[Memento]
의도:
캡슐화를 위배하지 않으면서 객체의 내부 상태를 파악하고 표현함으로써 객체의 상태를 저장해
둔 상태로 다시 복구 할 수 있게 한다.
다른 이름:
Token
-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//
// A Behavioral part of GoF's Design Patterns
//
// - Memento
//
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <deque>
#include <list>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
/////////////////////////////////////////////////////////////////////////////
//
// Utility
//
/////////////////////////////////////////////////////////////////////////////
template<class T>
struct deleteobject_functor : public std::unary_function<T, void>
{
public:
void operator()(T ptr)
{
delete ptr;
}
};
/////////////////////////////////////////////////////////////////////////////
//
// Memento
//
/////////////////////////////////////////////////////////////////////////////
typedef std::string State;
class Memento
{
private:
friend class Originator;
private:
Memento(void) : State_("empty")
{
}
public:
State GetState (void) const
{
return State_;
}
void SetState (State s)
{
State_ = s;
}
private:
State State_;
};
class Originator
{
public:
Originator (void) : State_("empty")
{
}
public:
Memento* CreateMemento (void)
{
Memento *m;
m = new Memento();
m->SetState (State_);
return m;
}
void SetMemento (const Memento* m)
{
State_ = m->GetState ();
delete m;
}
void SetState (std::string s)
{
State_ = s;
}
private:
State State_;
public:
friend std::ostream& operator<< (std::ostream&, const Originator&);
};
class CareTaker
{
public:
virtual ~CareTaker ()
{
std::for_each(History_.begin(), History_.end(), deleteobject_functor<Memento*>());
}
public:
void Push (Memento* m)
{
History_.push_back (m);
}
Memento* Pop (void)
{
Memento* m = 0;
if (!History_.empty())
{
m=History_.back();
History_.pop_back();
}
return m;
}
private:
std::deque <Memento*> History_;
};
////-------------------------------------------------------------------------
std::ostream& operator<< (std::ostream& os, const Originator &org)
{
os << "Originator's State=" << org.State_.c_str();
return os;
}
/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
Originator originator;
CareTaker caretaker;
originator.SetState ("first");
std::cout << originator << std::endl;
std::cout << std::endl;
caretaker.Push ( originator.CreateMemento () );
originator.SetState ("second");
std::cout << originator << std::endl;
caretaker.Push ( originator.CreateMemento () );
originator.SetState ("third");
std::cout << originator << std::endl;
std::cout << std::endl;
originator.SetMemento ( caretaker.Pop () );
std::cout << originator << std::endl;
originator.SetMemento ( caretaker.Pop () );
std::cout << originator << std::endl;
return 0;
}
-----------------------------------------------------------------------------
Originator's State=first
Originator's State=second
Originator's State=third
Originator's State=second
Originator's State=first