Memorandum mode of Design Pattern
Definition: capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
Structure:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yr7A/bt6wujuw.vcd4kpha + PHByZSBjbGFzcz0 = "brush: java;"> public class Memento {private String state; public Memento (String state) {this. state = state;} public String getState () {return state;} public class Originator {private String state; public String getState () {return state ;} public void setState (String state) {this. state = state;} public void setState (Memento memento) {this. state = memento. getState ();} public Memento createmento () {return new Memento (state);} public void show () {System. out. println ("State =" + state) ;}} public class Caretaker {private Memento memento; public Memento getMemento () {return memento;} public void setMemento (Memento memento) {this. memento = memento ;}}Client code:
public class Client {public static void main(String[] args) {Originator o = new Originator();o.setState("on");o.show();Caretaker c = new Caretaker();c.setMemento(o.createMemento());o.setState("off");o.show();o.setState(c.getMemento());o.show();}}The running result is as follows:
State = on
State = off
State = on