Memo Pattern-memento Pattern
Memento Memo design mode is an object that holds an internal state copy of another object, so that the object can be restored to a previously saved state.
The scenario in this article: There is a game can be archived at any time, after the archive is complete, you can read the data in the file, and then the next time you turn on the game can continue to play from that point.
There's a kid. The mechanism of the archive found a trick: he played a game for a while to see if he was in the game to make money or a loss.
If you earn money, then quickly archive; If you lose money, shut down and start over again from the previous archive.
In this way, the child will be able to make a profit, hahaha, next look at this memo mode it.
Gamer Class
This is the player class, which is the object that the memo is being played with.
import java.util.*;/** * Game Hero */public class Gamer {private static final Random Random = new Random (); /** * The amount of game held * */private int money; Public Gamer (int money) {This.money = money; } public int Getmoney () {//Gets the current money that is held in return; } public void Bet () {int dice = Random.nextint (3) + 1; if (dice = = 1) {money + = 100; System.out.println ("The money held has increased. "); } else if (dice = = 2) {Money-= Random.nextint (money-1) + 1; System.out.println ("The money held was reduced. "); } else {System.out.println ("nothing happened. "); }}//Create a memo as snapshot public Memento Creatememento () {Return to New Memento (money); }//Use Memo to recover data public void Restorememento (Memento Memento) {This.money = Memento.getmoney (); Public String toString () {return ' [money = ' + Money + '] '; }}
Memento class
A memo class that uses this class to store backup information for the gamer class.
/** * Memo, archive, or call snapshot */public class Memento { //The amount in the archive is private int money; public int Getmoney () { return money; } Public Memento (int money) { This.money = money; }}
Main
This is used to simulate scenarios, run programs
Import Memento.sample.game.gamer;import Memento.sample.game.memento;public class Main {public static void main (string[ ] args) {//The initial number of money held is Gamer Gamer = new Gamer (100); Save the original state Memento Memento = Gamer.creatememento (); for (int i = 0; i < i++) {//shows the current number of cycles System.out.println ("= = =" + i); Show the protagonist's current money Count System.out.println ("Present state:" + gamer); Play the game Gamer.bet (); System.out.println ("The money held is" + gamer.getmoney () + "Yuan. "); Decide what to do with memento,//If the coins increase, then continue//if the gold is reduced, then get the IF (Gamer.getmoney () > Memen from the memo To.getmoney ()) {System.out.println ("(the money held is increased so that the current state of the game is saved)"); Memento = Gamer.creatememento (); } else if (Gamer.getmoney () < Memento.getmoney ()) {System.out.println ("(the money held is reduced so the game reverts to its previous state)"); Gamer.restorememento (Memento); System.out.println ("The Money after Recovery is:" + gamer.getmoney () + "Yuan)"); } System.out.println (""); } }}
The results of the operation are relatively long, take a typical section to show:
Memo Pattern-memento pattern (Java implementation)