# Memento pattern exercise

**URL:** <https://forum.codewithmosh.com/t/memento-pattern-exercise/1746>\
**Category:** Design Patterns\
**Created:** [January 7, 2021, 5:11pm UTC](https://forum.codewithmosh.com/t/memento-pattern-exercise/1746 "2021-01-07T17:11:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![octopussy](https://avatars.discourse-cdn.com/v4/letter/o/b5ac83/32.png) [@octopussy](https://forum.codewithmosh.com/u/octopussy)\
**Post date:** [January 7, 2021, 5:11pm UTC](https://forum.codewithmosh.com/t/memento-pattern-exercise/1746/1 "2021-01-07T17:11:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ChrisUribe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/chrisuribe/32/62_2.png) [@ChrisUribe](https://forum.codewithmosh.com/u/ChrisUribe)\
**Post date:** [January 8, 2021, 10:37pm UTC](https://forum.codewithmosh.com/t/memento-pattern-exercise/1746/2 "2021-01-08T22:37:32Z")

</div>

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](https://www.baeldung.com/java-array-deque)_.

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

---

<div class="post-metadata">

**Author:** ![octopussy](https://avatars.discourse-cdn.com/v4/letter/o/b5ac83/32.png) [@octopussy](https://forum.codewithmosh.com/u/octopussy)\
**Post date:** [January 9, 2021, 10:38am UTC](https://forum.codewithmosh.com/t/memento-pattern-exercise/1746/3 "2021-01-09T10:38:06Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![ChrisUribe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/chrisuribe/32/62_2.png) [@ChrisUribe](https://forum.codewithmosh.com/u/ChrisUribe)\
**Post date:** [January 9, 2021, 6:38pm UTC](https://forum.codewithmosh.com/t/memento-pattern-exercise/1746/4 "2021-01-09T18:38:09Z")

</div>

1. There is a Demos, Exercises, and Solutions folder. If we look in the “solutions” folder, within the `History.java` 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](https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html), we see that push and pop would be the appropriate functions to use the Deque class as a stack.

 ![Java-deque-push](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/b1a3aeabc5207700d306b4530f982cbd75b7d462.png)

 ![Java-deque-pop](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/babc82ccc364363120aa10c421dc0a9a5bd2e3eb.png)

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](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html).

 ![ArrayList-add](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/fc0f7ef1301755f325fa8f481a9bd7d8536074af.png) ![ArrayList-Remove](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/352c5775c8d88a53a2559df8a34b2515f0545565.png)

Again, in the Exercises folder, History.java 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. 🙂

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) {
    states.add(state);
  }

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

    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.

---

<div class="post-metadata">

**Author:** ![ChrisUribe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/chrisuribe/32/62_2.png) [@ChrisUribe](https://forum.codewithmosh.com/u/ChrisUribe)\
**Post date:** [January 10, 2021, 10:04pm UTC](https://forum.codewithmosh.com/t/memento-pattern-exercise/1746/5 "2021-01-10T22:04:49Z")

</div>

One more thing, I would highly recommend taking Mosh’s _[The Ultimate Data Structures & Algorithms](https://codewithmosh.com/p/data-structures-algorithms)_ course. He has an entire section in part I that goes over Stacks in detail.
