Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C++] Proper memento implementation.

Design Patterns Memento Behavioral Pattern c++ c++ design pattern

  • Please log in to reply
2 replies to this topic

#1 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 08 September 2012 - 04:28 AM

Hi,

I've myself came up with memento design pattern implementation. Please tell me if it's right way to implement this.
Code is for VS 2010

memento.h
#pragma once
#include "stdafx.h"
class Memento {
private:
string state;
public :
Memento(string stateToSave) {
  state = stateToSave;
}
string get_saved_state() {
  return state;
}
};
class Originator {
private:
string state;
public :
void set (string state_) {
  cout << "\nSetting state to: "<<state_ << endl;
  state = state_;
}
Memento save_to_mem() {
  cout << "Saving to memento...\n";
  return Memento(state);
}
void restore_from_mem(Memento mem) {
  state = mem.get_saved_state();
  cout << "Restoring from memento to state :"
   << state << endl;
}
};

main.cpp:
#include "stdafx.h"
void memento_main() {
cout << " \tMemento\n";
Originator or;
vector<Memento> memVector;
or.set("State 1");
memVector.push_back(or.save_to_mem());
or.set("State 2");

Memento m = memVector.at(0);
memVector.pop_back();
or.restore_from_mem(m);
}

int _tmain(int argc, _TCHAR* argv[])
{
memento_main();
//system("Pause");
return 0;
}


I was trying to avoid nesting classes cause I have few big classes I don't want to modify but in some part of program objects of theese classes need to be restored to previous state. Eg. Shop in RPG game - User can put new armor on and check stats, and then buy or discard changes.
  • 0
Remebre about KISS & DRY

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 08 September 2012 - 05:07 AM

Well, this is what usually we do for implementing Memento pattern but originally we have to make Memento class such as that no one can create the Memento or alter the state of Memento other than the Originator.

Actually I'm talking about implementing as follows.

/*
* Memento, for now, saves the state only for var1 for Originator class.
* You can add more states depending Orginator needs.
*/
class Memento {
private:
int mVar1;

private:
/*
* We are making all the ways of creating a Memento private
* so that no one can create/modify state of a memento other than the Originator.
*/
Memento(){}
Memento(const Memento& source){ mVar1 = source.mVar1;}
Memento &operator=(const Memento &source){mVar1 = source.mVar1; return *this;}

void setVar1(int var) {mVar1 = var;}
int getVar1() const {return mVar1;}

/*
* Memento is declaring Originator as his friend so that Originator can
* create Memento or access its members.
*/
friend class Orginator;
};


class Orginator {
private:
int mVar1;
int mVar2;

public:
void setVar1(int var) {mVar1 = var;}
int getVar1() const {return mVar1;}

void setVar2(int var) {mVar2 = var;}
int getVar2() const {return mVar2;}

Memento* saveState()
{
Memento* memento = new Memento;
memento->setVar1(getVar1());
return memento;
}

void restoreState(Memento* memento)
{
setVar1(memento->getVar1());
}
};

void mementoExample()
{
Orginator* originator = new Orginator;
originator->setVar1(10);
originator->setVar1(11);

// create Memento
Memento* memento = originator->saveState();

// originator state
originator->setVar1(12);

// restore orignator state
originator->restoreState(memento);
}

  • 1

#3 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 08 September 2012 - 06:30 AM

Thank's for explanation.
I was thinking Is this good idea to make Memento class a Template class ? I won't have to write implementation two times, but is it still memento pattern ? Using friend operator won't be available but all members would be public... Im thinking about some kind of container for list of objects that later i could do "undo" couple times.
  • 0
Remebre about KISS & DRY





Also tagged with one or more of these keywords: Design Patterns, Memento, Behavioral, Pattern, c++, c++, design pattern

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download