Memo mode definition://Without destroying the encapsulation, capture the internal state of an object and save the state outside of the object. You can then restore the object to the original saved state//instance: Test two scenarios. The two scenarios in the first stage of the process are the same, the second stage is different//instance code//Memo object of the narrow interface public interface Flowamockmemento {//empty, the so-called narrow interface, that is, just an identity function, Its holder is not able to invoke no matter what its method}//test flow class public class Flowamock {private String flowname;private int tempresult;private string Tempstate;public Flowamock (String flowname) {this.flowname = Flowname;} public void Runphaseone () {tempresult = 3;tempstate = "Phaseone";} public void Shema1 () {this.tempstate + = ", Schema1"; System.out.pritnln (this.tempstate+ ": Now Run" +tempresult); This.tempresult + = 11;} public void Schema2 () {this.tempstate + = ", Schema2"; System.out.pritnln (this.tempstate+ ": Now Run" +tempresult); This.tempresult + = 22;} Public Flowamockmemento Creatememento () {return new Mementoimpl (This.tempresult, this.tempstate);} public void Setmemento (Flowamockmemento memento) {Mementoimpl Mementoimpl = (mementoimpl) Memento;this.tempresult = Mementoimpl.gettempresult (); this.tempstate = Mementoimpl.gettempstate ();} private static CLASS Memetoimpl implements Flowamockmemento{private int tempresult;private String tempstate;public memetoimpl (int Tempresult, String tempstate) {this.tempresult = Tempresult;this.tempstate = tempstate;} public int Gettempresult () {return tempresult;} public int gettempstate () {return tempstate;}}} Memo Management class public class Flowamementocaretaker {private Flowamockmemento memento = Null;public void Savememento ( Flowamockmemento memento) {this.memento = memento;} Public Flowamockmemento Retrivememento () {return this.memento;}} Clientpublic class Client {public static void main (string[] args) {flowamock mock = new Flowamock ("Testflow"); mock.runph Aseone (); Flowamementocaretaker caretaker = new Flowamementocaretaker (); Flowamockmemento memento = Mock.creatememento (); Caretaker.savememento (memento); mock.schema1 ();// Implementation of the first scenario Mock.setmemento (Caretaker.retrivememento ());//Recover Data mock.schema2 ();//Perform another kind of}}//Memo mode essence: Save and restore internal state// Strengths: Better encapsulation. Because of the use of internal classes and narrow interfaces, external even if holding memo interface object is not able to access the properties//Memo mode inside and narrow interface is the most force of design, so that the object's interface to the external save/There are no methods in the interface, so the external cannot be accessed. Fully guaranteed encapsulation//The primary skill required to create it is its value and get enough
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Getting started with memo mode design mode memento