Design Mode note memo mode Memento

Source: Internet
Author: User

Design Mode note memo mode Memento

 

// Memento memo mode-object behavior mode

 

/*

1: Intention:

Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, you can talk about this object later.

Restore to the saved status.

2: alias:

Token

3: motivation:

4: Applicability:

1> you must save the state of an object at a certain time point so that it can be restored to the previous state later.

2> If an interface is used to directly obtain these States for other objects, the implementation details of the object will be exposed and the object encapsulation will be broken.

5: structure:

Caretaker:

Originator:--> Memento: <--------- memento

SetMemento (Memento m) GetState ()

{State = m-> GetState ()} SetState ()

CreateMemento () state

{Retrun new Memento (state )}

State

6: participants:

1> Memento:

1) the memorandum stores the internal status of the primary machine. The primary node determines the internal states of the primary node as needed.

2) Prevent Access memos from objects other than the primary machine. There are actually two interfaces in the Memorandum. The management interface can only see the narrow description of the memorandum.

Interface -- it can only be used to deliver memos to other objects. On the contrary, the primary server can see a wide interface that allows it to access and return to the previous status.

All required data.

2> Originator

1) The primary machine creates a memorandum to record its internal status at the current time.

2) use the memorandum to restore the internal status.

3> Caretaker

1) saves the memorandum.

2) The content of the memorandum cannot be operated or inspected.

7: collaboration:

1> the manager requests a memorandum from the primary machine, saves it for a period of time, and sends it back to the primary machine.

2> passive in memorandum. Only the primary machine that creates a memo will assign values to and retrieve its status.

8: effect:

1> advantages:

1) protection of encapsulation boundaries:

Using memos can avoid exposing information that is only managed by the primary but must be stored outside the primary.

2) simplified the primary machine:

The State in which customers manage their requests simplifies the primary server, and customers do not need to notify the primary server at the end of their work.

2> disadvantages:

1) the usage of the memorandum may be costly:

If the primary machine must copy and store a large amount of information during the production of the memorandum, or the customer frequently creates a memorandum and restores the primary machine

Status, which may cause very large overhead.

2) potential costs of maintaining the Memorandum:

The manager is responsible for deleting the memos it maintains, and then the manager does not know how many States there are in the memos. Therefore, when you store memos,

A small manager may produce a large amount of storage overhead.

3> define narrow interfaces and wide interfaces:

In some languages, it may be difficult to ensure that only the primary machine can access the memorandum status.

9: Implementation:

1> language support:

The memorandum has two interfaces: one is the wide interface used by the primary object and the other is the narrow interface used by other objects.

In c ++, you can declare Originator as a friend of Memento and use the Memento wide interface as private, and declare the narrow interface as public.

2> incremental storage changes:

If the creation of a memorandum and the order in which it returns are predictable, the memorandum can only store incremental changes to the internal status of the primary machine.

10: Sample Code :*/

 

Class Graphic;

 

// Caretaker for operations

Class MoveCommand

{

Public:

MoveCommand (Graphic * target, const Point & delta );

Void Execute ();

Void Unexecute ();

Private:

ConstraintSolverMemento * _ state;

Point _ delta;

Graphic * _ target;

};

 

// The primary device provides creation, storage, and revocation operations.

Class ConstraintSolver

{

Public:

Static ConstraintSolver * Instance ();

 

Void Solve ();

Void AddConstraint (Graphic * startConnection, Graphic * endConnection );

Void RemoveConstaint (Graphic * startConnection, Graphic * endConnection );

ConstraintSolverMemento * CreateMemento ();

Void SetMemento (ConstraintSolverMemento *);

 

Private:

 

};

// Memorandum

Class ConstraintSolverMemento

{

Public:

Virtual ~ ConstraintSolverMemento ();

Private:

Friend class ConstraintSolver;

ConstraintSolverMemento ();

//... There are some states

};

 

// Create a memo with the primary machine, save it, and perform other operations

Void MoveCommand: Execute ()

{

ConstraintSolver * solver = ConstraintSolver: Instance ();

_ State = solver-> CreateMemento ();

_ Target-> Move (_ delta _);

Solver-> Solve ();

}

 

// Use the previously stored memo to restore the previous state.

Void MoveCommand: Unexecute ()

{

ConstraintSolver * solver = ConstraintSolver: Instance ();

_ Target-> Move (-_ delta );

Solver-> SetMemento (_ state );

Solver-> Solve ();

}

// This example cannot be restored more than two times. If you want to recover more than two times, you must use arrays or linked lists to store memos.

 

 

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.