Memento pattern exercise

In the solution of the memento pattern exercise, Mosh is using an ArrayDeque. But shouldn’t the pop method be removing the first item, while the task is to “undo” which means that states should be removed from the end?

I agree with you that in the Memento pattern, we should use a stack in order to pop and therefore remove the last item added. So, a Last In First Out (L-I-F-O) data structure would be appropriate in this case.

First, let’s reference a clear description of the ArrayDeque object:

An ArrayDeque (also known as an “Array Double Ended Queue”, pronounced as “ArrayDeck”) is a special kind of a growable array that allows us to add or remove an element from both sides. An ArrayDeque implementation can be used as a Stack (Last-In-First-Out) or a Queue(First-In-First-Out). - Baeldung, Introduction to the Java ArrayDeque.

As long as Mosh uses the ArrayDeque object as a stack, we’re all in agreement.

1 Like

Thanks for your help. But how to tell exactly which implementation he is using?

The only information I have obtained from the Internet so far is that if one wants to use the ArrayDeque as a stack, one should use push() instead of add(). However, Mosh is using add() both in his memento pattern (solution to exercise) and in his command pattern (course demo).

  1. There is a Demos, Exercises, and Solutions folder. If we look in the “solutions” folder, within the file (please reference lines 9-15) you’ll see that Mosh uses the push and pop methods. Also, if we take a look at the Java Documentation for Deque class, we see that push and pop would be the appropriate functions to use the Deque class as a stack.

In particular, push “pushes an element onto the stack … at the head.” Also, pop “removes and returns the first element … which is the top of the stack.”

  1. If you look in the Exercises folder he is now using an ArrayList class. As you mentioned, he is using add and remove. However, he is using this structure as if it were a stack. First, let’s go to the Java documentation to see a description of how add and remove work.

Again, in the Exercises folder, file, you’ll see that he is using a method named push to “add” an element to the end of the list. Then, he uses the method named pop to “remove” the last element on the list. So, that would be Last In First Out (L-I-F-O). So, this data structure is being used as a stack. :slight_smile:

UPDATE: I’m not sure how much code I can post from the course here, but this is for educational purposes. So, here goes:

  public void push(EditorState state) {

  public EditorState pop() {
    var lastIndex = states.size() - 1;
    var lastState = states.get(lastIndex);

    return lastState;

Just note that even though he’s using “add” and “remove” in the code above, he’s wrapping them in the methods push() and pop() (with additional code) so that they work like a stack.

1 Like

One more thing, I would highly recommend taking Mosh’s The Ultimate Data Structures & Algorithms course. He has an entire section in part I that goes over Stacks in detail.