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