A sample Memento pattern: Is it correct?

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?

Problem

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!

3

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.

6

I don’t fully agree with your statement about what the Memento pattern stands for.

The idea behind this pattern is that there is an object (“originator”) that has an internal state. There is another object (the “caretaker”) that wants to perform an action over the originator, but wants to be able to undo the change.

The first thing the Caretaker does is to ask for a Memento object to the Originator (which is a representation of its internal state) and then perform the operation which will change the internal state of the Originator. If the Caretaker is not happy with the result, it can revert the changes in the internal state of the Originator using the Memento. The Originator accepts the Memento and restores its internal state.

Therefore, what you save it’s the internal state of the Originator encapsulated in an object called Memento.

In your program the Caretaker should be the one to perform the operations over the Originator and not just a data structure to store states. The only requirement is to store the last Memento to support “Undo”. However you can support multiples “Undo” operations with any LIFO data structure where you can “pop” and “push” elements. Therefore, the second index is not needed, but I can imagine you can use it somehow to support “REDO” operations (and then it makes no sense in a zero player game).

In conclusion, I think that your design is wrong although it does make sense to apply the Memento pattern in this problem.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật