Design mode MEMENTO - system articles (original)

xiaoxiao2021-03-06  106

Design mode MEMENTO - system articles

People who often use a computer may not be unfamiliar with the system backup. When your Windows system runs normally, it is backup it. When the system is running, you can call the backup quickly to recover the system, so you can A large amount of saving the pain of reloading the system, especially when you lack a certain drive, or when you have a blame problem, it is still painful. People I want to have this experience should be very good, huh, huh!

Ok, let's take a look at how this process is implemented:

1, let us first define the Windows System (WindowsSystem) class:

Public class windowssystem {

PRIVATE STRING State;

Public MEMENTO CREATEMEMENTO () {// Create a backup, save the current state

Return New MEMENTO (State);

}

Public Void RestoreMemento (MEMENTO MEMENTO) {// Restore system from backup

THIS.STATE = MementO.getState ();

}

Public string getState () {// Get status

Return this.state;

}

Public void setState (String State) {// setting status

THIS.STATE = State;

System.out.println ("The current system is in" this.state);

}

}

2, then define the backup (MEMENTO) class:

Public class memento {

PRIVATE STRING State;

Public Memento (String State) {// Backup

THIS.STATE = State;

}

Public string getState () {// Get status

Return this.state;

}

Public void setState (String State) {// setting status

THIS.STATE = State;

}

}

3. Define the user (USER) class:

PUBLIC CLASS User {

Private Memento MEMENTO;

Public Memento RetrieveMemento () {// Restore System

Return this.Memento;

}

Public Void SaveMemento (MEMENTO MEMENTO) {// Save System

THIS.MEMENTO = MEMENTO;

}

}

4, write test classes:

Public class test {

Public static void main (string args []) {

Windowssystem WinXP = new windowssystem (); // WinXP system

User User = new user (); // a user

Winxp.setState ("Good State"); // WinXP is in a good running state

User.saveMemento (WinXP.createMemento ()); // User backups for the system, WinXP system to generate backup files

Winxp.setState ("bad state"); // WinXP is in a bad operation

WinXp.restoreMemento (user.RetrieveMemento ()); // User Send Recovery Command, System Recovery

System.out.println ("Current System is in" WinXP.getState ());

}

5. Description:

A: Definition: The MEMENTO object is an object that saves another object's internal status, so that the object can be restored to the originally saved state.

The use of Memento mode is to capture the status of an object without destroying the package, and is external, stored, so that this object is restored to the stored state.

C: The role involved in the MEMENTO mode has three, memo roles, initiator roles, and person in charge.

Memorandum role:

(1) Store the internal state of the initiator object, the memo can determine the internal state of the number of initiator objects based on the judgment of the initiator object.

(2) The memo can protect its contents not read any object other than the initiator object.

The role of sponsor character:

(1) Create a memo object containing the current internal state.

(2) Use the memo object to store its internal state.

The role of the person in charge:

(1) Responsible for saving the memo object.

(2) Do not check the contents of the memo object.

D: In this example, the MEMENTO class is a memo role, and the Windows System class is the initiator role, the user (USER) class is the person in charge.

转载请注明原文地址:https://www.9cbs.com/read-103341.html

New Post(0)