
Memento

Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
Source code
/** * Creates a memento containing a snapshot of its current internal state. * Uses the memento to restore its internal state. * @role __Originator */ public class Originator { public Memento createMemento() { return new ConcreteMemento(this); } public void setMemento(Memento memento) { if (memento instanceof ConcreteMemento) { // extract state from memento } } } /** * Represents narrow interface of the memento visible to Caretaker * @role __Memento */ public interface Memento { } /** * Stores internal state of the Originator object. * This is sort of wide memento interface, * visible to Originator. */ public class ConcreteMemento implements Memento { public ConcreteMemento(Originator originator) { // initialize memento with originator's state } }