Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Following this query on memento pattern, I have tried to put my understanding to test.

Memento pattern stands for three things:

  1. Saving state of the "memento" object for its successful retrieval
  2. Saving carefully each valid "state" of the memento
  3. Encapsulating the saved states from the change inducer so that each state remains unaltered

Have I achieved these three with my design?

This is a zero player game where the program is initialized with a particular set up of chess pawns - the knight and queen. Then program then needs to keep adding set of pawns or knights and queens so that each pawn is "safe" for the next one move of every other pawn.

The condition is that either both pawns should be placed, or none of them should be placed. The chessboard with the most number of non conflicting knights and queens should be returned.

Implementation I have 4 classes for this:

  1. protected ChessBoard (the Memento)

    private int [][] ChessBoard;
    public void ChessBoard();
    protected void SetChessBoard();
    protected void GetChessBoard(int);
  2. public Pawn This is not related to memento. It holds info about the pawns

    public enum PawnType: int
        Empty = 0,
        Queen = 1,
        Knight = 2,
    //This returns a value that shown if the pawn can be placed safely
    public bool IsSafeToAddPawn(PawnType);
  3. public CareTaker This corresponds to caretaker of memento

    This is a double dimentional integer array that keeps a track of all states. The reason for having 2D array is to keep track of how many states are stored and which state is currently active. An example:

    0 -2
    1 -1
    2 0 - This is current state. With second index 0/
    3 1 - This state has been saved, but has been undone

    private int [][]State;
    private ChessBoard [] MChessBoard;
    //This gets the chessboard at the position requested and assigns it to originator
    public ChessBoard GetChessBoard(int);
    //This overwrites the chessboard at given position
    public void SetChessBoard(ChessBoard, int);
    private int [][]State;
  4. public PlayGame (This is the originator)

    private bool status;
    private ChessBoard oChessBoard;
    //This sets the state of chessboard at position specified public
    SetChessBoard(ChessBoard, int);
    //This gets the state of chessboard at position specified public
    ChessBoard GetChessBoard(int);
    //This function tries to place both the pawns and returns the status of this attempt
    public bool PlacePawns(Pawn);

Edit after discussion with Kevin

The way I am trying to apply design patterns to my problems is not correct. In a way it helps me understand "how" to "implement" it. Yes. But then, this is not its ideal usage. I might as well simplify my "learning" problem and use my text book ones.

Also refer to this question on programmers.stackoverflow and all the discussions!

share|improve this question
Code Review has decided that it's not a good fit for them, so it'll be staying here. I think that this is right on the border line of design and implementation, and since design questions are on-topic here and Code Review says there's not enough for them, I don't see a good reason to close it. –  Thomas Owens Aug 28 '12 at 18:13
@ThomasOwens, Do you think it would be better if I put a UML diagram up instead? –  TheSilverBullet Aug 29 '12 at 2:11
That would probably be better, considering there's no implementation of any of the methods shown. The appropriate diagrams would probably be good. But it's up to you. –  Thomas Owens Aug 29 '12 at 10:22
add comment

1 Answer

up vote 2 down vote accepted

You don't need a Memento pattern for this problem, just simple backtracking. I didn't quite understand your use of 'pawn', but basically you put something in the first legal spot. You only need to remember the current position and the best so far. When you can't put anything else on the board, then save the position if it is better than the previous best. Then undo the last placement and try the next spot.

share|improve this answer
thanks for your comment. I was hoping to do the backtracking in a more elegant way with this pattern. Basically removing a pawn from a position would convert itself to restoring a previous state. I do have some overkill in my design. I will rectify and update it shortly. –  TheSilverBullet Aug 29 '12 at 2:10
@TheSilverBullet: have you confused 'pawn' with man? Knights and Queens are pieces but not pawns. –  kevin cline Aug 29 '12 at 5:35
@ Kevin, Ah! Yes. I used pawn in place of piece... –  TheSilverBullet Aug 29 '12 at 7:34
If the memento pattern is a bit of an overkill for this, do you think there is any other design pattern that would be right here? –  TheSilverBullet Sep 10 '12 at 8:16
@TheSilverBullet: Don't look for patterns to apply before writing code. Instead wait until the code gets ugly, i.e. you start repeating yourself. If you don't know how to clean it up, see if there is a design pattern that will help. OO design patterns don't help with small algorithmic programs. If you were writing a lot of programs similar to this one (e.g. queens placement, knights tour), you would start repeating the backtracking algorithm. Then you might extract the backtracking algorithm into a template class, or switch to a language that supports functional programming. –  kevin cline Sep 10 '12 at 18:41
show 1 more comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.