Jane said design pattern - memo mode

First, what is the memo mode

  This memorandum we should not unfamiliar vocabulary, I often use the memo to record some of the more important or easily forgotten information, the most common applications associated with many, such as saved games, we play the game when certainly archiving when the next time you log in the game can be designed to continue the game from where it left off, or to archive the resurrection point, if you can hang up the archive information read resurrection points to start again. With a similar database is transaction rollback or redo log redo log and so on.

  Memo mode (Memento) , without violating encapsulation, capture and the internal state of an object, and holds the state outside the object. So that later you can restore the object to a previously saved state. UML structure is as follows:

  Which, Originator is a sponsor, is responsible for creating a memorandum of Memento, the current time to record its internal state and can be used to restore internal memo states; Memento is a memorandum, the internal state is responsible for storing Originator object and prevents other than the Originator Object access memorandum Memento; Caretaker managers, responsible for keeping good memorandum of Memento, not the contents of the memorandum of operation or inspection.

  1. The sponsor role

  Record the internal state of the current time, and is responsible for creating and restoring memo data, allowing access to all the data is returned to the previous desired state.

 1 public class Originator {
 2     
 3     private String state;
 4 
 5     public String getState() {
 6         return state;
 7     }
 8 
 9     public void setState(String state) {
10         this.state = state;
11     }
12     
13     public Memento createMento() {
14         return (new Memento(state));
15     }
16     
17     public void setMemento(Memento memento) {
18         state = memento.getState();
19     }
20     
21     public void show() {
22         System.out.println("state = " + state);
23     }
24     
25 }

  2. Memorandum role

  Responsible for internal state storage Originator object sponsor, the sponsor needs to provide internal state when needed.

 1 public class Memento {
 2     
 3     private String state;
 4     
 5     public Memento(String state) {
 6         this.state = state;
 7     }
 8     
 9     public String getState() {
10         return state;
11     }
12 
13 }

  3. Memorandum Administrator role

  Memorandum on the management, conservation and provide a memo, the memo can only be passed to other roles.

 1 public class Caretaker {
 2     
 3     private Memento memento;
 4 
 5     public Memento getMemento() {
 6         return memento;
 7     }
 8 
 9     public void setMemento(Memento memento) {
10         this.memento = memento;
11     }
12     
13 }

  4. Client Client

  Write the following piece of code to test that first state is set to On, and then save the status is set to Off, and then restore the original state by memorandum Administrator role.

 1 public class Client {
 2     
 3     public static void main(String[] args) {
 4         Originator originator = new Originator();
 5         originator.setState("On");    //Originator初始状态
 6         originator.show();
 7         
 8         Caretaker caretaker = new Caretaker();
 9         caretaker.setMemento(originator.createMento());
10         
11         originator.setState("Off");    //Originator状态变为Off
12         originator.show();
13 is          
14          originator.setMemento (caretaker.getMemento ());     // return the initial state 
15          originator.show ();
 16      }
 . 17  
18 is }

  Results are as follows:

  

Application Second, the memo mode

  1. When to Use

  • To record the internal state of an object in order to allow the user to cancel uncertain or incorrect operation, can be restored to its original state

  2. Methods

  • Storing a memo by specialized class object state

  3. advantage

  • To provide users with a mechanism to restore the state, you can use more convenient to be able to return to a state history
  • To achieve a package of information, such that the user need not be concerned about the details of the state of preservation of

  4. shortcomings

  • Consume resources

  5. usage scenarios

  • You need to save and restore data related scenes
  • Providing a rollback operation, such as ctrl + z, the browser back button, the Backspace key, etc.
  • Need to monitor the copy of the scene

  6. Application Examples

  • Game Archive
  • ctrl + z keys, backspace, etc. browser (undo / reduction)
  • Board games undo
  • Roll back the database transaction

  7. Notes

  • In order to comply with the Law of Demeter, the need for a management memo class
  • Do not use the memo mode frequently create a backup scenario. To save memory, the prototype model may be used memo mode +

Third, to achieve the memo mode

  Below saved games as an example, look at how to use the memo mode. UML is as follows:

  1. game character

  Simply record the game character vitality, attack power, defense power, to save the current state by saveState () method to restore the role of the state by recoveryState () method.

. 1  public  class GameRole {
 2      
. 3      Private  int VIT;     // Health 
. 4      Private  int ATK;     // attack 
. 5      Private  int DEF;     // defense 
. 6      
. 7      public  int getVit () {
 . 8          return VIT;
 . 9      }
 10      public  void setVit ( int VIT) {
 . 11          the this .vit = VIT;
 12 is      }
 13 is      public  int getAtk() {
14         return atk;
15     }
16     public void setAtk(int atk) {
17         this.atk = atk;
18     }
19     public int getDef() {
20         return def;
21     }
22     public void setDef(int def) {
23         this.def = def;
24     }
25     
26     //状态显示
27     public void stateDisplay() {
28         System.out.println ( "role of the current status:" );
 29          System.out.println ( "Physical:" + the this .vit);
 30          System.out.println ( "attack:" + the this .atk);
 31          System.out.println ( "defense:" + the this .def);
 32          System.out.println ( "-----------------" );
 33 is      }
 34 is      
35      / / obtaining the initial state of the 
36      public  void getInitState () {
 37 [          the this .vit = 100 ;
 38 is          the this .atk = 100 ;
 39          the this .def = 100 ;
40      }
 41 is      
42 is      // after the battle 
43 is      public  void Fight () {
 44 is          the this .vit = 0 ;
 45          the this .atk = 0 ;
 46 is          the this .def = 0 ;
 47      }
 48      
49      // save the character status 
50      public RoleStateMemento the saveState ( ) {
 51 is          return ( new new RoleStateMemento (VIT, ATK, DEF));
 52 is      }
 53 is      
54 is      // restore the character status 
55      public  void recoveryState(RoleStateMemento memento) {
56         this.vit = memento.getVit();
57         this.atk = memento.getAtk();
58         this.def = memento.getDef();
59     }
60 
61 }

  2. The role of the state storage box

  Memorandum class for storing character status.

. 1  public  class RoleStateMemento {
 2      
. 3      Private  int VIT;     // Health 
. 4      Private  int ATK;     // attack 
. 5      Private  int DEF;     // defense 
. 6      
. 7      public RoleStateMemento ( int VIT, int ATK, int DEF) {
 . 8          the this . = VIT VIT;
 . 9          the this .atk = ATK;
 10          the this .def = DEF;
 . 11      }
 12 is 
13      public  int getVit () {
 14          return vit;
15      }
 16  
17      public  void setVit ( int vit) {
 18          this .vit = vit;
19      }
 20  
21      public  int getAtk () {
 22          return the computer;
23      }
 24  
25      public  void setAtk ( int computer) {
 26          this .atk = computer;
27      }
 28  
29      public  int getDef() {
30         return def;
31     }
32 
33     public void setDef(int def) {
34         this.def = def;
35     }
36     
37 }

  3. The role of state managers

  Memorandum managers.

 1 public class RoleStateCaretaker {
 2     
 3     private RoleStateMemento memento;
 4 
 5     public RoleStateMemento getMemento() {
 6         return memento;
 7     }
 8 
 9     public void setMemento(RoleStateMemento memento) {
10         this.memento = memento;
11     }
12 
13 }

  4. Client Client

  The following write a simple program to test, write logical roughly the archive before the boss fight, fight boss failed to read files.

 1 public class Client {
 2     
 3     public static void main(String[] args) {
 4         //打boss前
 5         GameRole gameRole = new GameRole();
 6         gameRole.getInitState();
 7         gameRole.stateDisplay();
 8         
 9         //保存进度
10         RoleStateCaretaker caretaker = new RoleStateCaretaker();
11         caretaker.setMemento(gameRole.saveState());
12         
13         //打boss失败
14         gameRole.fight();
15         gameRole.stateDisplay();
16         
17         //恢复状态
18         gameRole.recoveryState(caretaker.getMemento());
19         gameRole.stateDisplay();
20     }
21 
22 }

  Results are as follows:

  

 

  Source Address: https://gitee.com/adamjiangwh/GoF  

Reproduced in: https: //www.cnblogs.com/adamjwh/p/11018268.html

Guess you like

Origin blog.csdn.net/weixin_34099526/article/details/93160843
Recommended