1. What is the Memento Pattern
The Memento Pattern is a behavioral design pattern used to save and restore the internal state of an object. It saves the object’s state to a memento object, which can be used to restore the object to a previous state in the future.
2. Characteristics and Advantages of the Memento Pattern
The characteristics and advantages of the Memento Pattern include:
- It can save and restore the internal state of an object without violating the encapsulation principle.
- It can flexibly manage the historical states of an object, making it convenient to perform undo and redo operations.
- It allows the state to be saved externally, avoiding the exposure of the object’s internal state.
3. Examples of Practical Applications of the Memento Pattern
The Memento Pattern has many practical application scenarios, some examples of which include:
- The undo and redo functionality in text editors, which can use the Memento Pattern to save the state of each operation.
- The save and load functionality in games, which can use the Memento Pattern to save the game progress.
- The draft storage feature in email clients, which can use the Memento Pattern to save the state of draft emails.
4. Implementation of the Memento Pattern in Golang
4.1. UML Class Diagram
4.2. Example Introduction
Description Based on the UML Class Diagram
In this example, we have an Originator class with an internal state called “state”. The Originator sets the state using the SetState method and creates a memento object using the CreateMemento method. The internal state of the memento object is the same as that of the Originator. The Caretaker class is responsible for storing the memento object and adding mementos using the AddMemento method.
Implementation Steps
- Create the Memento object, which has a method GetState to save the internal state of the Originator.
- Create the Originator object, which has methods to set the state and create a memento.
- Create the Caretaker object, which is responsible for saving the memento object.
- Implement the methods to create a memento and restore the state in the Originator:
- When creating a memento object, save the state of the Originator in the memento.
- When restoring the state, restore the state from the memento to the Originator.
- Implement the methods to add a memento and get a memento in the Caretaker:
- The add memento method is used to save the memento object in the Caretaker.
- The get memento method is used to retrieve the memento object from the Caretaker.
4.3.1. Create a Memento Object
type Memento struct {
state string
}
func (m *Memento) GetState() string {
return m.state
}
4.3.2. Store State in the Memento
type Originator struct {
state string
}
func (o *Originator) SetState(state string) {
o.state = state
}
func (o *Originator) CreateMemento() *Memento {
return &Memento{state: o.state}
}
4.3.3. Restore State from the Memento
func (o *Originator) SetMemento(memento *Memento) {
o.state = memento.GetState()
}
4.3.4. Using the Memento Pattern for Undo Operations
type Caretaker struct {
mementos []*Memento
}
func (c *Caretaker) AddMemento(m *Memento) {
c.mementos = append(c.mementos, m)
}
func (c *Caretaker) GetMemento(index int) *Memento {
return c.mementos[index]
}
4.3.5. Using the Memento Pattern for Redo Operations
func main() {
originator := &Originator{}
caretaker := &Caretaker{}
originator.SetState("State 1")
caretaker.AddMemento(originator.CreateMemento())
originator.SetState("State 2")
caretaker.AddMemento(originator.CreateMemento())
originator.SetMemento(caretaker.GetMemento(0))
fmt.Println("Originator state after restoring to state 1:", originator.state)
originator.SetMemento(caretaker.GetMemento(1))
fmt.Println("Originator state after restoring to state 2:", originator.state)
}