# Command Pattern Exercise: Using addFirst() method fixes wrong undo order

**URL:** <https://forum.codewithmosh.com/t/command-pattern-exercise-using-addfirst-method-fixes-wrong-undo-order/9506>\
**Category:** Design Patterns\
**Created:** [December 28, 2021, 9:31pm UTC](https://forum.codewithmosh.com/t/command-pattern-exercise-using-addfirst-method-fixes-wrong-undo-order/9506 "2021-12-28T21:31:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkPurpleKnight](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@DarkPurpleKnight](https://forum.codewithmosh.com/u/DarkPurpleKnight)\
**Post date:** [December 28, 2021, 9:31pm UTC](https://forum.codewithmosh.com/t/command-pattern-exercise-using-addfirst-method-fixes-wrong-undo-order/9506/1 "2021-12-28T21:31:11Z")

</div>

Hi

In the Command Pattern, Mosh uses a `Deque` instead of a `List`. For adding new objects he uses the `add()` method, which is available for both the `Deque` and the `List`.

I did it the same way in the Command Pattern exercise. As I added several `Command` objects to my `History` object one after the other and then tried to undo them in the correct order with an `UndoCommand` object, I noticed that the order was not correct.

Finally, when I used the [`addFirst()`](https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#addFirst-E-) method to add objects to my `History`, everything worked as expected.

**Code:** See [gist](https://gist.github.com/DarkPurpleKnight/e88505d1317dcbcb50836a25ea7b153d).

**Question:** Has anyone else noticed this and/or have I done something wrong?

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 11, 2022, 6:01am UTC](https://forum.codewithmosh.com/t/command-pattern-exercise-using-addfirst-method-fixes-wrong-undo-order/9506/2 "2022-08-11T06:01:27Z")

</div>

At least currently, Mosh’s solution file implements it like this using `push` and `pop`:

```java
package com.codewithmosh.command;

import java.util.ArrayDeque;
import java.util.Deque;

public class History {
    private Deque<UndoableCommand> commands = new ArrayDeque<>();

    public void push(UndoableCommand command) {
        commands.push(command);
    }

    public UndoableCommand pop() {
        return commands.pop();
    }

    public int size() {
        return commands.size();
    }
}

```

In the video, he did use `add` which I just assumed added things to the end of the Deque. That should also work. Personally, I would just use the `Stack` class which has more obvious semantics, but if it works, it works.

* * *

> [@DarkPurpleKnight](#):
>
> **Code:** See [gist](https://gist.github.com/DarkPurpleKnight/e88505d1317dcbcb50836a25ea7b153d).

AFAICT, that code is buggy. When you call `pop()` you are popping things off from the end of the Deque. That means you are undoing things in the order that they were done rather than undoing the most recently done thing.

For example, if I did three actions A, B, and C your history would look like this: [C, B, A]. Then calling pop() would produce A, then B, then C (so acting like a FIFO queue). The way undo mechanisms works, we would want to undo C, then undo B, then undo A (acting like a LIFO stack). So your implementation seems to be doing things in the exact opposite order from what they should be doing.

Cheers!
