Memento

Definition: The memento pattern provides the ability to return to a previous state.

Usage: Use the memento pattern to store a previous state. This is essentially just saving data.

Example

class GameState {
	//store all the game data in here somehow
}

class Game {
	GameState getCurrentState() {return gameState;}

	restoreState(GameState savedState) {gameState = savedState;}

	GameState gameState
}

The GameState is the Memento used by the Game. The I/O isn't included above, but saving the game would only require writing the GameState to a file. Loading a game would require reading the GameState from a file and then calling Game.restoreState.

Index

Copyright (C) 2008 Steven Fletcher. All rights reserved.