mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 04:44:36 +03:00
code refactoring: fix memory leaks, code style, etc.
This commit is contained in:
@@ -16,19 +16,21 @@
|
||||
* stores internal state of the Originator object and protects
|
||||
* against access by objects other than the originator
|
||||
*/
|
||||
class Memento {
|
||||
class Memento
|
||||
{
|
||||
private:
|
||||
// accessible only to Originator
|
||||
friend class Originator;
|
||||
|
||||
Memento(int s)
|
||||
: state(s) {}
|
||||
|
||||
void setState(int s) {
|
||||
|
||||
Memento( const int s ) : state( s ) {}
|
||||
|
||||
void setState( const int s )
|
||||
{
|
||||
state = s;
|
||||
}
|
||||
|
||||
int getState() {
|
||||
|
||||
int getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
// ...
|
||||
@@ -43,25 +45,30 @@ private:
|
||||
* creates a memento containing a snapshot of its current internal
|
||||
* state and uses the memento to restore its internal state
|
||||
*/
|
||||
class Originator {
|
||||
class Originator
|
||||
{
|
||||
public:
|
||||
// implemented only for printing purpose
|
||||
void setState(int s) {
|
||||
void setState( const int s )
|
||||
{
|
||||
std::cout << "Set state to " << s << "." << std::endl;
|
||||
state = s;
|
||||
}
|
||||
|
||||
|
||||
// implemented only for printing purpose
|
||||
int getState() {
|
||||
int getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
void setMemento(Memento *m) {
|
||||
|
||||
void setMemento( Memento* const m )
|
||||
{
|
||||
state = m->getState();
|
||||
}
|
||||
|
||||
Memento *createMemento() {
|
||||
return new Memento(state);
|
||||
|
||||
Memento *createMemento()
|
||||
{
|
||||
return new Memento( state );
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -73,55 +80,68 @@ private:
|
||||
* CareTaker
|
||||
* is responsible for the memento's safe keeping
|
||||
*/
|
||||
class CareTaker {
|
||||
class CareTaker
|
||||
{
|
||||
public:
|
||||
CareTaker(Originator *o)
|
||||
: originator(o) {}
|
||||
|
||||
~CareTaker() {
|
||||
for (unsigned int i = 0; i < history.size(); i++) {
|
||||
delete history.at(i);
|
||||
CareTaker( Originator* const o ) : originator( o ) {}
|
||||
|
||||
~CareTaker()
|
||||
{
|
||||
for ( unsigned int i = 0; i < history.size(); i++ )
|
||||
{
|
||||
delete history.at( i );
|
||||
}
|
||||
history.clear();
|
||||
}
|
||||
|
||||
void save() {
|
||||
std::cout << "Save state." << std::endl;;
|
||||
history.push_back(originator->createMemento());
|
||||
|
||||
void save()
|
||||
{
|
||||
std::cout << "Save state." << std::endl;
|
||||
history.push_back( originator->createMemento() );
|
||||
}
|
||||
|
||||
void undo() {
|
||||
std::cout << "Undo state." << std::endl;;
|
||||
originator->setMemento(history.back());
|
||||
|
||||
void undo()
|
||||
{
|
||||
if ( history.empty() )
|
||||
{
|
||||
std::cout << "Unable to undo state." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Memento *m = history.back();
|
||||
originator->setMemento( m );
|
||||
std::cout << "Undo state." << std::endl;
|
||||
|
||||
history.pop_back();
|
||||
delete m;
|
||||
}
|
||||
// ...
|
||||
|
||||
private:
|
||||
Originator *originator;
|
||||
std::vector<Memento *> history;
|
||||
std::vector<Memento*> history;
|
||||
// ...
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
Originator *originator = new Originator;
|
||||
CareTaker *caretaker = new CareTaker(originator);
|
||||
|
||||
originator->setState(1);
|
||||
Originator *originator = new Originator();
|
||||
CareTaker *caretaker = new CareTaker( originator );
|
||||
|
||||
originator->setState( 1 );
|
||||
caretaker->save();
|
||||
|
||||
originator->setState(2);
|
||||
|
||||
originator->setState( 2 );
|
||||
caretaker->save();
|
||||
|
||||
originator->setState(3);
|
||||
|
||||
originator->setState( 3 );
|
||||
caretaker->undo();
|
||||
|
||||
|
||||
std::cout << "Actual state is " << originator->getState() << "." << std::endl;
|
||||
|
||||
|
||||
delete originator;
|
||||
delete caretaker;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user