Memento pattern

One example of how this can be used is to restore an object to its previous state (undo via rollback), another is versioning, another is custom serialization.

To roll back to the state before the operations, it returns the memento object to the originator.

Classic examples of the memento pattern include a pseudorandom number generator (each consumer of the PRNG serves as a caretaker who can initialize the PRNG (the originator) with the same seed (the memento) to produce an identical sequence of pseudorandom numbers) and the state in a finite state machine.

(1) createMemento() by creating and returning a Memento object that stores originator's current internal state and

The following Java program illustrates the "undo" usage of the memento pattern.

A sample UML class and sequence diagram for the Memento design pattern. [ 1 ]