This article describes the use of memo mode to implement the level-passing game functions of Java. Share it for your reference, as follows:
1. Pattern definition
Memo mode captures the internal state of an object without destroying the enclosure and saves this state outside the object. In this way, the object can be restored to its original saved state later.
2. Examples of the model
1 Mode Analysis
We borrowed the level-through game to illustrate this mode.
2 Memo mode static class diagram
3 code examples (black box memo mode)
3.1 Create memo narrow interface INarrowMemento
package com.demo.memento;/** * Memorandum narrow interface (no method is provided, external objects cannot access internal information of the Memorandum object) * * @author * */public interface INarrowMemento {}3.2 Memorandum sponsor Hero
package com.demo.originator;import java.util.Random;import com.demo.memento.INarrowMemento;/** * Challenger* * @author * */public class Hero { // blood value private int blood; // force value private int sword; // random number private final Random random = new Random(); // construct method initialize content public Hero() { this.blood = 100; this.sword = 100; } // create memorandum to save content public INarrowMemento createMemento() { System.out.println("Create memorandum..."); return new Memento(this.blood, this.sword); } // Restore memorandum content public void restoreFromMemento(INarrowMemento memento) { System.out.println("Restore the status in the memorandum..."); if (memento != null) { Memento memento2 = (Memento) memento; this.blood = memento2.getBlood(); this.sword = memento2.getSword(); } } /** * Challenge BOSS */ public int koBoss() { // When the blood value is <=0, the challenge fails. Assume that the probability of defeating the BOSS is 2% // When judging, there is still a blood value if (this.blood <= 0 || this.sword <= 0) { System.out.println(this.toString()); System.out.println("Change the BOSS failed!"); return -1; } else { // Get the random number double win = Math.random(); if (win <= 0.02) { System.out.println(this.toString()); System.out.println("Congratulations, challenge the boss successfully!"); return 1; } else { System.out.println(this.toString()); System.out.println("Continue to attack the boss..."); // Random number reduces blood value and force value to continue KO int blood_sub = random.nextInt(10); int sword_sub = random.nextInt(10); this.blood -= blood_sub; this.sword -= sword_sub; return 0; } } } @Override public String toString() { return "Current blood value: " + this.blood + " - Current force value: " + this.sword; } /** * Memorandum (the entire class is private and can only be accessed by the initiator) * * @author * */ private class Memento implements INarrowMemento { // blood value private final int blood; // force value private final int sword; // constructor initialization content private Memento(int blood, int sword) { this.blood = blood; this.sword = sword; } private int getBlood() { return blood; } private int getSword() { return sword; } }}3.3 Memorandum Manager - Caretaker
package com.demo.caretaker;import com.demo.memento.INarrowMemento;/** * Administrator* * @author * */public class Caretaker { private INarrowMemento memento; /** * Get memorandum object* * @return */ public INarrowMemento getMemento() { return memento; } /** * Save memorandum object* * @param memento */ public void setMemento(INarrowMemento memento) { this.memento = memento; }}3.4 Let the hero challenge the boss one client
package com.demo;import com.demo.caretaker.Caretaker;import com.demo.originator.Hero;/** * Client main application* * @author * */public class Client { /** * @param args */ public static void main(String[] args) { // Create role Hero hero = new Hero(); // Create administrator Caretaker caretaker = new Caretaker(); // Save the status information before the challenge caretaker.setMemento(hero.createMemento()); // There are only three chances to defeat the boss int cnt = 1; // Challenge BOSS result int ko = -1; while (ko != 1 && cnt <= 3) { System.out .println("=====================" + cnt + "Challenge ==================================================================================================================================================================== { if (ko == -1) { // Challenge failed and restored to initial state Cumulative challenge times hero.restoreFromMemento(caretaker.getMemento()); cnt += 1; break; } else if (ko == 0) { // Continue to challenge ko = hero.koBoss(); } else if (ko == 1) { // The challenge is successful! break; } } } } }}4 Running results
Create a memo...
=======================================
Current blood value: 100 - Current force value: 100
Continue to attack the boss...
Current blood value: 96 - Current force value: 99
Continue to attack the boss...
Current blood value: 90 - Current force value: 98
Continue to attack the boss...
Current blood value: 81 - Current force value: 95
Continue to attack the boss...
Current blood value: 78 - Current force value: 93
Continue to attack the boss...
Current blood value: 72 - Current force value: 88
Continue to attack the boss...
Current blood value: 64 - Current force value: 85
Continue to attack the boss...
Current blood value: 56 - Current force value: 80
Continue to attack the boss...
Current blood value: 49 - Current force value: 73
Continue to attack the boss...
Current blood value: 45 - Current force value: 71
Continue to attack the boss...
Current blood value: 37 - Current force value: 68
Continue to attack the boss...
Current blood value: 29 - Current force value: 65
Continue to attack the boss...
Current blood value: 20 - Current force value: 59
Continue to attack the boss...
Current blood value: 11 - Current force value: 54
Continue to attack the boss...
Current blood value: 9 - Current force value: 52
Continue to attack the boss...
Current blood value: 3 - Current force value: 45
Continue to attack the boss...
Current blood value: -3 - Current force value: 41
Failed to challenge the boss!
Restore status in the memo...
====================== 2nd challenge================
Current blood value: 100 - Current force value: 100
Continue to attack the boss...
Current blood value: 96 - Current force value: 95
Continue to attack the boss...
Current blood value: 96 - Current force value: 91
Continue to attack the boss...
Current blood value: 88 - Current force value: 82
Continue to attack the boss...
Current blood value: 79 - Current force value: 79
Continue to attack the boss...
Current blood value: 76 - Current force value: 72
Continue to attack the boss...
Current blood value: 73 - Current force value: 70
Continue to attack the boss...
Current blood value: 72 - Current force value: 66
Continue to attack the boss...
Current blood value: 72 - Current force value: 61
Continue to attack the boss...
Current blood value: 72 - Current force value: 58
Continue to attack the boss...
Current blood value: 72 - Current force value: 52
Continue to attack the boss...
Current blood value: 63 - Current force value: 51
Continue to attack the boss...
Current blood value: 62 - Current force value: 50
Continue to attack the boss...
Current blood value: 54 - Current force value: 41
Continue to attack the boss...
Current blood value: 50 - Current force value: 39
Continue to attack the boss...
Current blood value: 47 - Current force value: 39
Continue to attack the boss...
Current blood value: 43 - Current force value: 38
Continue to attack the boss...
Current blood value: 37 - Current force value: 36
Continue to attack the boss...
Current blood value: 34 - Current force value: 35
Continue to attack the boss...
Current blood value: 32 - Current force value: 27
Continue to attack the boss...
Current blood value: 28 - Current force value: 22
Continue to attack the boss...
Current blood value: 26 - Current force value: 15
Continue to attack the boss...
Current blood value: 24 - Current force value: 11
Continue to attack the boss...
Current blood value: 19 - Current force value: 3
Continue to attack the boss...
Current blood value: 10 - Current force value: -3
Failed to challenge the boss!
Restore status in the memo...
==================== The third challenge==================
Current blood value: 100 - Current force value: 100
Continue to attack the boss...
Current blood value: 99 - Current force value: 93
Continue to attack the boss...
Current blood value: 98 - Current force value: 84
Continue to attack the boss...
Current blood value: 98 - Current force value: 82
Continue to attack the boss...
Current blood value: 95 - Current force value: 76
Continue to attack the boss...
Current blood value: 88 - Current force value: 68
Continue to attack the boss...
Current blood value: 81 - Current force value: 64
Continue to attack the boss...
Current blood value: 76 - Current force value: 64
Continue to attack the boss...
Current blood value: 67 - Current force value: 64
Congratulations, you have successfully challenged the boss!
III. The design principles of this pattern
1. Maintaining the boundary of the package
2 dual interface implementation to ensure security.
4. Use occasions
1 When it is necessary to restore an object's previous state at a certain moment.
2. In white box memo mode, you need to save the state of an object at a certain moment externally, but if you use an interface to allow other objects to directly obtain these states, it will expose the implementation details of the object and destroy the encapsulation of the object.
3 Black box memo mode implementation provides a dual interface access mechanism, providing a wide interface to the initiator object, and narrow interface to objects other than the initiator, thus effectively solving the encapsulation and security.
V. Static class diagram
1 White box memo mode static class diagram
2 Black box memo mode static class diagram
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.