dimanche 4 mars 2018

What is the difference between Memento and Command design pattern?

I have successfully coded a solitaire game on Java, and now I have been asked to implement an undo/redo on top of my design.

My plan was to store a list or stack of moves the user executed, and if the user wants to undo, I would 1. check if the user can undo (ie. there are moves in the list or stack), then 2. reverse the last two moves I stored (ie. the "from" move where I moved the card from and the "to" move, where I moved the card to).

For redo, I would just redo the moves, depending on how far down the user did the undo action (for example, if they pressed undo twice, I would be, at least, (size of list - 4) down my list or stack).

I think they will be implemented in an interface like so:

public interface UndoRedo {
    void undo();
    void redo();
 }

Am I implementing Memento, or Command design pattern, or neither? I'm having trouble grasping what the two design patterns look like in the context of an undo/redo for this game. I'm also a beginner with Java OOP, and design patterns in general.

Aucun commentaire:

Enregistrer un commentaire