Select Page

Memento

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         }  }  
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Categories

0
Would love your thoughts, please comment.x
()
x