Momento Demo
package MementoPattern;
//This is our Memento
public class EditorState {
private final String content;
public EditorState(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
The EditorState
class serves as the Memento and is responsible for storing the state of an editor. Let's break down the code:
The code snippet above demonstrates the following:
The class is declared within the
MementoPattern
package.The
EditorState
class has a single attribute,content
, which represents the state of the editor. The attribute is marked asfinal
, indicating that it cannot be changed after initialization.The constructor
EditorState(String content)
is used to create an instance of theEditorState
class. It takes thecontent
parameter, which represents the state to be stored in the Memento.The
getContent()
method is a getter method that returns the stored content/state.
The purpose of this EditorState
class is to encapsulate and store the state of an editor at a specific point in time. It follows the Memento pattern by providing a way to capture and retrieve the state of an object without exposing its internal details. This allows the editor object to save and restore its state using instances of the EditorState
class, which can be managed by a caretaker object (not shown in the provided code).
package MementoPattern;
//This is out Originator
public class Editor {
private String content;
public EditorState createState() {
return new EditorState(content);
}
public void restore(EditorState state) {
content = state.getContent();
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
The provided code represents an Originator class in the context of the Memento design pattern.
Let's break down the code:
The code snippet above demonstrates the following:
The class is declared within the
MementoPattern
package.The
Editor
class serves as the Originator, which is the object whose state needs to be saved and restored.The
Editor
class has an attributecontent
, which represents the current state of the editor.The
createState()
method creates anEditorState
object that encapsulates the current state of the editor. It returns an instance of theEditorState
class, passing the currentcontent
as a parameter.The
restore(EditorState state)
method takes anEditorState
object and restores the state of the editor to the state captured in theEditorState
object. It retrieves the content/state from theEditorState
object and assigns it to thecontent
attribute of theEditor
class.The
getContent()
method is a getter method that returns the current content/state of the editor.The
setContent(String content)
method is a setter method that allows updating the content/state of the editor.
The purpose of this Editor
class is to serve as the Originator, which is responsible for creating Memento objects (EditorState
) that capture the state of the editor, as well as restoring the state of the editor from a given Memento. It encapsulates the state within the content
attribute and provides methods to create and restore Memento objects.
package MementoPattern;
import java.util.ArrayList;
import java.util.List;
//This is out Caretaker
public class History {
//We could actually use a stack here - but I have chosen to use a LIST
private List<EditorState> states = new ArrayList<>();
public void push(EditorState state) {
states.add(state);
}
public EditorState pop() {
var lastIndex = states.size() - 1;
var lastState = states.get(lastIndex);
states.remove(lastState);
return lastState;
}
}
The provided code represents a Caretaker class in the context of the Memento design pattern.
Let's break down the code:
The code snippet above demonstrates the following:
The class is declared within the
MementoPattern
package.The
History
class serves as the Caretaker, responsible for storing and managing the Memento objects (EditorState
).The
History
class maintains a list,states
, which stores instances ofEditorState
.The
push(EditorState state)
method is used to add a newEditorState
object to the list of states. It takes anEditorState
parameter and adds it to thestates
list.The
pop()
method retrieves the most recentEditorState
object from the list, removes it from the list, and returns it. The last state in thestates
list is obtained using the indexlastIndex
. ThelastState
is then removed from the list usingstates.remove(lastState)
, and finally,lastState
is returned.
The purpose of this History
class is to serve as the Caretaker, responsible for managing the Memento objects (EditorState
). It provides methods to add new states (push
) and retrieve the most recent state (pop
). The states
list acts as a storage for Memento objects, allowing the Originator (Editor
) to save and restore its state.
Note: The implementation in the History
class uses a simple List
to store the states. Although a stack data structure is typically used for the Caretaker in the Memento pattern, the code snippet opts for an ArrayList
. However, it is important to note that using a List
does not affect the essence of the Memento pattern's concept and functionality.
Driver:
The provided code demonstrates the usage of the Memento design pattern in a simple scenario. Let's break it down:
The code snippet above demonstrates the following:
The
MementoDriver
class serves as the entry point for the program.The
Editor
andHistory
classes are instantiated to create an editor and manage its state history.The
Editor
instance,editor
, is initially created.The
setContent(String content)
method of theEditor
is used to set the content/state of the editor to "Hello".The
createState()
method of theEditor
is called to create anEditorState
object representing the current state of the editor. ThisEditorState
is then added to theHistory
usinghistory.push()
.Similar steps are repeated to set and save two more states for the editor: "World!" and "Welcome to OOP Design".
The
restore(EditorState state)
method of theEditor
is used to restore the state of the editor to the most recently saved state in theHistory
. This is done by popping the last saved state from theHistory
usinghistory.pop()
.The content of the editor after each restoration is printed using
System.out.println(editor.getContent())
.
Overall, this code demonstrates the use of the Memento pattern to save and restore the state of an editor using the Editor
and History
classes. It shows how the editor's content/state can be changed, saved, and restored from the history using the Memento design pattern's principles and components.