Strange Implentation of Memento pattern in Design Course

Hello all!

I’m so surprised to see the difference in the Exercise and the Implementation of the memento pattern. This really doesn’t seem right. I haven’t continue the rest of the course yet; however, I wanted to sure this here:

The exercise said to make a method on the Editor class to then say editor.undo()

However this is the Implementation:

Is this actually how this pattern is used in a class?

I was surprised as well, and would be glad if Mosh could elaborate on that.
I thought that the calls to History should have been encapsulated inside functions like Undo() or Redo() in the Editor class. I don’t think the Main class should be aware of the History class. Am I wrong? @programmingwithmosh

1 Like

Hey! Thanks for replying.

Yeah I’m unsure how it would the Editor class would get the undo method with this pattern.

This is the best answer I found so far:

This is also a cool implementation of it :slight_smile: enough!

Take a look at this example code:

That clarified it for me.

Heyhey! I also hope that Mosh can clarify. The way he’s implemented also you’ll notice that he didn’t add the last state change to history, so when he restored, he got “b”, because “c” was not manually added. But in the GOF pattern, it doesn’t seem like originator interacts with history at all and it does make sense for the Document to keep track of state in my opinion. I hope @programmingwithmosh reads our comments and clarifies for us.