Example: the memento pattern


show more The Memento design pattern is a simple but creative programming approach to undoing a change to an object. It involves creating an object with the Memento class that simply details how to go back to the state before the change was made. The solution works because it's set up in a way that means the object still manages itself. See the solution explained in more detail in this online video "Example: the memento pattern." show less
please wait ...

Example: the memento pattern

Next let's take a look at the Memento Design Pattern. This is a way of managing change, basically a way to undo a change to an object, but doing it in a way that does not violate encapsulation, and the idea that an object should always manage itself. I am going to illustrate this one without code. This Memento Design Pattern requires three classes that take certain roles, and we'll call them the Originator, the Caretaker, and the Memento. So we have the Originator. This is the object that you want to be able to change, and then undo any changes to.

For you this could be a customer object, a spaceship object, whatever needs to be able to reverse out a change. But the question is who is changing it, who is affecting it? Well, then we have the object we refer to as the Caretaker. This object is going to deal with taking care of when and why the Originator needs to either save its state or to revert back to a previous state. Now, the Caretaker is often also the object that will actually be chang...

Example: the memento pattern
Video duration: 3m 6s 3h 1m Intermediate

Viewers:

The Memento design pattern is a simple but creative programming approach to undoing a change to an object. It involves creating an object with the Memento class that simply details how to go back to the state before the change was made. The solution works because it's set up in a way that means the object still manages itself. See the solution explained in more detail in this online video "Example: the memento pattern."

Subject:
Developer
Software:
Java
Author:
please wait ...