Memento
is one of the less popular GoF patterns, that is solving a niche but important problem - how to reset the state of an object to a specified historical state. Examples include: hitting Cancel
in a dialog, transaction rollback, etc.
The below code shows how to implement mementos in idiomatic Java, with minimum boiler plate and taking advantage of the features of a modern IDE (I've used IntelliJ IDEA, if anybody creates an Eclipse version let me know - I will link to it).
- Start with a data class like this one:
class DialogModel {