Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Memento Pattern

Memento Pattern

The Memento Pattern (Memento Pattern) saves a certain state of an object so that the object can be restored when appropriate; the Memento Pattern belongs to behavioral patterns.

The Memento Pattern allows capturing and restoring an object's internal state without breaking encapsulation.

Introduction

Intent

Capture an object's internal state without breaking encapsulation, and allow these states to be saved and restored outside the object.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

structures

The Memento Pattern contains the following main roles:

Create a class that implements

The Memento Pattern uses three classes Memento、Originator and CareTaker. Memento contains the state of the object to be restored. Originator creates and stores the state in the Memento object. The Caretaker object is responsible for restoring the object's state from the Memento.

MementoPatternDemo, and our demo class uses the CareTaker and Originator object to display the restoration of the object's state.

Step 1

Create the Memento class.

Memento.java

public

class

Memento

{

private

String

state

;

public

Memento

(

String

state

)

{

this

.

state

=

state

;

}

public

String

getState

(

)

{

return

state

;

}

}

Step 2

Create the Originator class.

Originator.java

public

class

Originator

{

private

String

state

;

public

void

setState

(

String

state

)

{

this

.

state

=

state

;

}

public

String

getState

(

)

{

return

state

;

}

public

Memento

saveStateToMemento

(

)

{

return

new

Memento

(

state

)

;

}

public

void

getStateFromMemento

(

Memento

Memento

)

{

state

=

Memento

.

getState

(

)

;

}

}

Step 3

Create the CareTaker class.

CareTaker.java

import

java

.

util

.

ArrayList

;

import

java

.

util

.

List

;

public

class

CareTaker

{

private

List

<

Memento

mementoList

=

new

ArrayList

<

Memento

(

)

;

public

void

add

(

Memento

state

)

{

mementoList

.

add

(

state

)

;

}

public

Memento

get

(

int

index

)

{

return

mementoList

.

get

(

index

)

;

}

}

Step 4

Use CareTaker and Originator object.

MementoPatternDemo.java

public

class

MementoPatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

Originator

originator

=

new

Originator

(

)

;

CareTaker

careTaker

=

new

CareTaker

(

)

;

originator

.

setState

(

"

State #1

"

)

;

originator

.

setState

(

"

State #2

"

)

;

careTaker

.

add

(

originator

.

saveStateToMemento

(

)

)

;

originator

.

setState

(

"

State #3

"

)

;

careTaker

.

add

(

originator

.

saveStateToMemento

(

)

)

;

originator

.

setState

(

"

State #4

"

)

;

System

.

out

.

println

(

"

Current State:

"

originator

.

getState

(

)

)

;

originator

.

getStateFromMemento

(

careTaker

.

get

(

0

)

)

;

System

.

out

.

println

(

"

First saved State:

"

originator

.

getState

(

)

)

;

originator

.

getStateFromMemento

(

careTaker

.

get

(

1

)

)

;

System

.

out

.

println

(

"

Second saved State:

"

originator

.

getState

(

)

)

;

}

}

Step 5

Verify the output.



Current State: State #4

First saved State: State #2

Second saved State: State #3