Memo Pattern-memento pattern (Java implementation)

Source: Internet
Author: User

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)

Related Article

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.