Java Design Pattern Application--Memo mode

Source: Internet
Author: User

Memo mode is primarily used for archiving. In the game we always play boss before the archive, if the boss failed to read the archive, re-challenge boss.

As you can see, the memo pattern typically includes the following data structures

1. Archive file: Necessary data for restoring the backup scene;

2, Archive Manager: For the management of archives, including the archiving of reading and writing and display;

3. The object being archived.

The following is an example of a shooting game archive:

1. Shooting player

 PackageCom.coshaho.learn.memorandum;//Shooting Hand Public classShooter {//Blood Volume    Private intblood = 100; //Number of bullets    Private intBullet = 100; //Shooting     Public voidshoot () {bullet= Bullet-(int) (Math.random () *30); if(0 >bullet) {Bullet= 0;    } bosshurt (); }        //Boss Damage    Private voidBosshurt () {Blood= Blood-(int) (Math.random () *25); if(0 >blood) {Blood= 0; }    }         Public voidShow () {System.out.println ("Shooter Blood is" + Blood + ", bullet is" + bullet + '. "); }    //Save     PublicMemorandum Save () {return Newmemorandum (Blood, Bullet); }        //Gta5-In     Public voidRecovery (memorandum memory) {if(NULL==memory) {            return; } Blood=Memory.getblood (); Bullet=Memory.getbullet (); }}

2. Archive files

 PackageCom.coshaho.learn.memorandum;//Archive Public classMemorandum {Private intBlood; Private intBullet;  PublicMemorandum (intBloodintbullet) {         This. Blood =Blood;  This. Bullet =Bullet; }     Public intGetblood () {returnBlood; }     Public intGetbullet () {returnBullet; }}

3. Archive Manager

 Package Com.coshaho.learn.memorandum; // Archive Manager  Public class Memorandummanager {    private  memorandum memory;      Public Memorandum GetMemory ()     {        return  memory;    }      Public void setmemory (memorandum memory)     {        this. Memory = memory;    }} 

4. Test code

 Package Com.coshaho.learn.memorandum;  Public class memorandumtest {    publicstaticvoid  main (string[] args)    {         New  Memorandummanager ();         New shooter ();        Shooter.shoot ();        Shooter.show ();        Mmemorymanager.setmemory (Shooter.save ());                Shooter.shoot ();        Shooter.show ();        Shooter.recovery (Mmemorymanager.getmemory ());                Shooter.show ();    }}

Java design mode application-Memo mode

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.