Getting started with memo mode design mode memento

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.