Memorandum (owner of design)

The owner memorandum is a Patron of design Logiciel which provides the manner of returning an object in a preceding state (return postpones) without violating the principle of encapsulation.

The memorandum is used by two objects: the creator and the guard. The creator is an object having an internal state (state to be safeguarded). The guard will act on the creator, while preserving the possibility of retrogressing. The guard then requests from the creator the memorandum object. He carries out the operation (or sequence of operations) desired. In order to allow the back return before the operations, the memorandum is turned over to the creator. The memorandum object even is opaque (the guard cannot, or would not have, to modify it). During use of this owner, a very detailed attention must be taken to check if the creator modifies other objects or resources - the owner memorandum must operate on only one object.

It should be stressed that the fact of safeguarding the state interns creative object must be carried out without breaking the principle of encapsulation. That is not always possible (example: SmallTalk does not allow it in a direct way).

Traditional examples of the owner memorandum include the generator of pseudo-random numbers, the machine in finished states, the function " Annulation" /" Undo".

Example

This example programmed in Java illustrates the use of the pattern Memento to carry out a type order of cancel.

importation java.util.*; class Originator { private String state; public void set (String state) { System.out.println (" Originator: state affects a: " +state); this.state = state; } public Object saveToMemento () { System.out.println (" Originator: safeguard in the memento."); return new Memorandum (state); } public void restoreFromMemento (Object m) { yew (m instanceof Memorandum) { Memorandum memorandum = (Memorandum) m; state = memento.getSavedState (); System.out.println (" Originator: State after restoration: " +state); } } private static class Memorandum { private String state; public Memorandum (String stateToSave) {state = stateToSave; } public String getSavedState () {return state; } } } class Caretaker { private ArrayList savedStates = new ArrayList (); public void addMemento (Object m) {savedStates.add (m); } public Object getMemento (int index) {return savedStates.get (index); } } class MementoExample { public static void hand (String args) { Caretaker caretaker = new Caretaker (); Originator originator = new Originator (); originator.set (" State1"); originator.set (" State2"); caretaker.addMemento (originator.saveToMemento ()); originator.set (" State3"); caretaker.addMemento (originator.saveToMemento ()); originator.set (" State4"); originator.restoreFromMemento (caretaker.getMemento (1)); } }

The result is: Originator: state affects a: State1 Originator: state affects a: State2 Originator: safeguard in the memorandum. Originator: state affects a: State3 Originator: safeguard in the memorandum. Originator: state affects a: State4 Originator: State after restoration: State3

Random links:Límite (matemáticas) | Saint-Julien-on-Calonne | Thiénans | Megapodius eremita | ACT New Zealand | Antoine Pastorana | Harry_Williams