The first post code is free to write content.
Memo 1
1 //A simple memo that can only record the state of the previous modification and implement the recall operation. 2 classstudent{3 PrivateString name;4 PrivateString age;5 PrivateString gender;6 //set up a student instance directly in the student class to store the previous state of the class object7 PrivateStudent memento=NULL;8 //The constructor is not verbose.9 PublicStudent () {}Ten PublicStudent (String n,string a,string g) { OneName =N; AAge =A; -Gender =G; - } the //when calling a function that might change the properties of a class, call Savememento to back up the pre-modified instance. - Public voidSetName (String N) { - Savememento (); -Name =N; + } - PublicString GetName () { + returnname; A } at //just like the top, no more wordy. - Public voidSetage (String a) { - Savememento (); -Age =A; - } - PublicString getage () { in returnAge ; - } to Public voidSetgender (String g) { + Savememento (); -Gender =G; the } * PublicString Getgender () { $ returngender;Panax Notoginseng } - //new An instance of this class for backing up the current instance the Private voidSavememento () { +Memento =NewStudent ( This. Name, This. Age, This. gender); A } the //back to back up + PublicStudent Getmemento () { - returnMemento; $ } $ PublicString toString () { - return"Name:" +name+ "Age:" +age+ "Gender:" +gender; - } the}
Test class
1 Public classtest{2 Public Static voidMain (string[] args) {3Student Tom =NewStudent ("Tom", "+", "M");4 5SYSTEM.OUT.PRINTLN ("initial message:" +Tom);6 //call the Set function, Tom has changed7Tom.setgender ("F");8System.out.println ("post-Edit info:" +Tom);9 //Tom went back to the state before the change.TenTom =Tom.getmemento (); OneSystem.out.println ("Recall before modification:" +Tom); A - -SYSTEM.OUT.PRINTLN ("initial message:" +Tom); theTom.setname ("Jarry"); -System.out.println ("post-Edit info:" +Tom); -Tom =Tom.getmemento (); -System.out.println ("Recall before modification:" +Tom); + } -}
Memo 2
1 //This memo can be withdrawn multiple times. 2 ImportJava.util.Stack;3 classstudent{4 PrivateString name;5 PrivateString age;6 PrivateString gender;7 //set up a stack of storage student instances directly in the student class to store backups of that class of objects8 PrivateStack<student> memento =NULL;9 //The constructor is not verbose.Ten PublicStudent () {} One PublicStudent (String n,string a,string g) { AName =N; -Age =A; -Gender =G; theMemento =NewStack<student>(); - } - //when calling a function that might change the properties of a class, call Savememento to back up the pre-modified instance. - Public voidSetName (String N) { + Savememento (); -Name =N; + } A PublicString GetName () { at returnname; - } - //just like the top, no more wordy. - Public voidSetage (String a) { - Savememento (); -Age =A; in } - PublicString getage () { to returnAge ; + } - Public voidSetgender (String g) { the Savememento (); *Gender =G; $ }Panax Notoginseng PublicString Getgender () { - returngender; the } + //Push the current state of an instance into a memo A Private voidSavememento () { theMemento.push (NewStudent ( This. Name, This. Age, This. gender)); + } - //instead of returning a new student, the pop-out is an instance of the memo, with its properties assigned to this property $ PublicStudent Getmemento () { $Student Prememento =Memento.pop (); -Name =prememento.getname (); -Age =prememento.getage (); theGender =Prememento.getgender (); - return This;Wuyi } the PublicString toString () { - return"Name:" +name+ "Age:" +age+ "Gender:" +gender; Wu } -}
Test class
Public classtest{ Public Static voidMain (string[] args) {Student Tom=NewStudent ("Tom", "+", "M"); System.out.println ("Initial message:" +Tom); //call the Set function, Tom has changedTom.setgender ("F"); System.out.println ("Post-Modification information:" +Tom); Tom.setname ("Jarry"); System.out.println ("Post-Modification information:" +Tom); Tom.setage ("17"); System.out.println ("Post-Modification information:" +Tom); Tom.setname ("Haha"); System.out.println ("Post-Modification information:" +Tom); //Tom went back to the state before the change.System.out.println ("Recall before modification:" +Tom.getmemento ()); System.out.println ("Recall to modify before:" +Tom.getmemento ()); System.out.println ("Recall to modify before:" +Tom.getmemento ()); System.out.println ("Recall to modify before:" +Tom.getmemento ()); }}
Memento3
Design Pattern 03 Memo (Java)