Memento mode (memorandum design mode)

Source: Internet
Author: User
Memento mode?

When you use object-oriented Programming to Implement the revocation function, you need to save the status information of the instance in advance. Then, you need to restore the instance to its original state based on the saved information. In this case, you need to use the memento design mode. (And save the instance status)

  • Keywords:
    1. Undo (UNDO)
    2. · Redo (redo)
    3. · History (History)
    4. · Snapshot)
  • Damage encapsulation:
    The Code dependent on the internal structure of the instance is dispersed in various places in the program, making the program difficult to maintain.

  • Wide and narrow Interface
  1. Wide interface-wide interface (APL) the "wide interface (API)" provided by the memento role is a set of all methods used to retrieve the object state information. Since the wide interface (API) Exposes the internal information of all memento roles, only the originator role can be used for the wide interface (API.
  2. Narrowinterface-the narrow interface (API) memento role provides the "narrow interface (API)" for external caretaker roles )". Memento roles that can be obtained through narrow APIs have limited internal information, so it can effectively prevent information leakage.
    By providing the above two APIs, You can effectively preventEncapsulation damaged
  • Related Design Patterns
    1. In command mode (Chapter 1), you can use memento mode to undo commands in command mode.
    2. In the protype mode (Chapter 1), in the memento mode, the current state of the object is saved to implement the snapshot and revocation functions. The saved information is only the part of the information required for restoring the status.
    In protype mode, another instance that is identical to the current instance is generated. The two instances have the same content.
  1. State mode (Chapter 1) in memento mode, "instance" is used to indicate the state. In the state mode, the "class" is used to represent the State.
Clarify responsibilities
  • Implement Functions
  1. · The game is automatically played.
  2. · The hero of the game decides the next state by throwing a dice
  3. · When the number of dice points is 1, the money of the hero increases. When the number of dice points is 2, the money of the hero decreases.
  4. · When the number of dice is 6, the master's guild will receive fruit
  5. · When no money is available, the game ends.

Package ==>>>> name ====>>> description
Game | memento | class indicating the gamer status
Game | gamer indicates the class of the game hero. It generates memento instances for the game class. It saves the memento instance in advance, and then restores the gamer status as needed.
Null | Maint. For convenience, use Maint as the owner to save the user status.

UML

Sequence diagram:

Code
  • Gamer
The money and fruits in the public class gamer {/*** are defined according to the general definition method. * but when we extract memento, pay attention to this acquisition rule. * // get money. private int money; // obtain the fruit private list <string> fruits = new arraylist <> (); Private random = new random (); private Final Static string [] fruitname = new string [] {"apple", "grape", "banana", "orange"}; Public gamer (INT money) {This. money = money;} public int getmoney () {return money;}/*** start game * dice result 1, 2, 6 */Public void bet () {int dice = random. nextint (6) + 1; if (Dice = 1) {This. money ++ = 100; system. out. println ("money has increased! ");} Else if (Dice = 2) {This. Money/= 2; system. Out. println (" the money is halved! ");} Else if (Dice = 6) {string F = getfruit (); system. Out. println (" obtained fruit ["+ F +"]! "); This. fruits. add (f);} else {system. out. println ("Nothing happens");}/*** snapshot Method */Public memento creatememento () {memento = new memento (this. money); iterator <string> iterator = fruits. iterator (); While (iterator. hasnext () {string S = iterator. next (); If (S. startswith ("delicious") {memento. addfruit (s) ;}} return memento;}/*** undo Method */Public void restorememento (memento) {This. money = memento. money; this. fruits = memento. fruits;} private string getfruit () {string prefix = ""; if (random. nextboolean () {prefix = "delicious";} return prefix + fruitname [random. nextint (fruitname. length)] ;}@ override Public String tostring () {return "gamer {" + "Money =" + money + ", fruits =" + fruits + '}';}}
  • Memento
When using the public class memento {/***, because memento and gamer are strongly correlated, but because they are in the same game package, * using the visibility modifier is important: * The two fields in the same package can access */INT money; arraylist <string> fruits;/*** narrow interface */Public int getmoney () {return money;}/*** the wide interface * @ Param money */memento (INT money) {This. money = money; this. fruits = new arraylist <> () ;}/ *** the wide interface */void addfruit (string fruit) {fruits. add (fruit);}/*** here is the wide interface */arraylist <string> getfruits () {return (arraylist <string>) fruits. clone ();}}
  • Maint
Public class Maint {/*** the status here is only a single snapshot point. When you need multiple snapshot points, * Create a snapshot class separately for management and use the set, * write an example here */public static void main (string [] ARGs) {gamer = new gamer (100); // save a snapshot of the initial state memento = gamer. creatememento (); For (INT I = 0; I <100; I ++) {system. out. println ("=" + I); system. out. println ("current state" + gamer); // start the game gamer. bet (); system. out. println ("How much is there" + gamer. getmoney () + "RMB"); If (gamer. getmoney ()> memento. getmoney () {system. out. println ("// save New state"); memento = gamer. creatememento ();} else if (gamer. getmoney () <memento. getmoney ()/2) {system. out. println ("Money is reduced by half and restored to the original state"); gamer. restorememento (memento);} Try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace () ;}}} public class snapshotmanger implements serializable {private list <memento> mementos = new arraylist <> ();/*** implement Java. io. serializable interface * use the writeobject method of objectoutputstream * use the readobject method of objectinputstream * // *** save * // *** restore */}

Memento mode (memorandum design 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.