From the course: Java Design Patterns: Behavioral Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understanding the Memento pattern

Understanding the Memento pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 1

Start my 1-month free trial

Understanding the Memento pattern

- [Instructor] Say you want to create an undo operation and to do that, you need to save the states of an object but you don't want to break the principle of encapsulation. The memento pattern allows you to do exactly this. There are some cases where you need to keep a record of the states of an object. For example, say you're writing an the text documents and at some point you click save and then you reword what you've written. But later you decide you want to go back to the previous version. You would need to have a record of the state that document was in before you saved it. This is what the memento pattern does. It's externalizes the state of an object at a given time in case he needs to get back to it later. But the main challenge is how you do this without breaking encapsulation. When I looked at the mediator pattern in the previous chapter, I talked to lots about encapsulation. One of the benefits of encapsulation…

Contents