docs: update memento

This commit is contained in:
Ilkka Seppälä
2024-05-07 16:23:10 +03:00
parent df578ce58d
commit 1b04adadb8
3 changed files with 41 additions and 42 deletions
+35 -17
View File
@@ -3,34 +3,36 @@ title: Memento
category: Behavioral
language: en
tag:
- Gang of Four
- Encapsulation
- Gang of Four
- Memory management
- Object composition
- State tracking
- Undo
---
## Also known as
Token
* Snapshot
* Token
## Intent
Without violating encapsulation, capture and externalize an object's internal state so that the
object can be restored to this state later.
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
## Explanation
Real-world example
> We are working on an astrology application where we need to analyze star properties over time. We
> are creating snapshots of star states using the Memento pattern.
> We are working on an astrology application where we need to analyze star properties over time. We are creating snapshots of star states using the Memento pattern.
In plain words
> Memento pattern captures object internal state making it easy to store and restore objects in any
> point of time.
> Memento pattern captures object internal state making it easy to store and restore objects in any point of time.
Wikipedia says
> The memento pattern is a software design pattern that provides the ability to restore an object to
> its previous state (undo via rollback).
> The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
**Programmatic Example**
@@ -43,12 +45,11 @@ public enum StarType {
WHITE_DWARF("white dwarf"),
SUPERNOVA("supernova"),
DEAD("dead star");
...
// ...
}
```
Next, let's jump straight to the essentials. Here's the `Star` class along with the mementos that we
need to manipulate. Especially pay attention to `getMemento` and `setMemento` methods.
Next, let's jump straight to the essentials. Here's the `Star` class along with the mementos that we need to manipulate. Especially pay attention to `getMemento` and `setMemento` methods.
```java
public interface StarMemento {
@@ -110,7 +111,7 @@ public class Star {
private int massTons;
// setters and getters ->
...
// ...
}
}
```
@@ -162,12 +163,29 @@ sun age: 10000000 years mass: 500000 tons
Use the Memento pattern when
* A snapshot of an object's state must be saved so that it can be restored to that state later, and
* A direct interface to obtaining the state would expose implementation details and break the
object's encapsulation
* A direct interface to obtaining the state would expose implementation details and break the object's encapsulation
## Known uses
* [java.util.Date](http://docs.oracle.com/javase/8/docs/api/java/util/Date.html)
* Java Util Package: Various classes in the Java Util Package, such as java.util.Date and java.util.Calendar, can be reverted to previous states using similar principles, though not implemented directly as the Memento Pattern.
* Undo mechanisms in software: Text editors and graphic editors often use this pattern to implement undo actions.
## Consequences
Benefits:
* Preserves encapsulation boundaries.
* Simplifies the originator by removing the need to manage version history or undo functionality directly.
Trade-offs:
* Can be expensive in terms of memory if a large number of states are saved.
* Care must be taken to manage the lifecycle of mementos to avoid memory leaks.
## Related Patterns
* [Command](https://java-design-patterns.com/patterns/command/): Often used together; commands store state for undoing operations in mementos.
* [Prototype](https://java-design-patterns.com/patterns/prototype/): Mementos may use prototyping to store the state.
## Credits
@@ -67,7 +67,7 @@ public class App {
states.add(star.getMemento());
star.timePasses();
LOGGER.info(star.toString());
while (states.size() > 0) {
while (!states.isEmpty()) {
star.setMemento(states.pop());
LOGGER.info(star.toString());
}
@@ -24,6 +24,9 @@
*/
package com.iluwatar.memento;
import lombok.Getter;
import lombok.Setter;
/**
* Star uses "mementos" to store and restore state.
*/
@@ -85,34 +88,12 @@ public class Star {
/**
* StarMemento implementation.
*/
@Getter
@Setter
private static class StarMementoInternal implements StarMemento {
private StarType type;
private int ageYears;
private int massTons;
public StarType getType() {
return type;
}
public void setType(StarType type) {
this.type = type;
}
public int getAgeYears() {
return ageYears;
}
public void setAgeYears(int ageYears) {
this.ageYears = ageYears;
}
public int getMassTons() {
return massTons;
}
public void setMassTons(int massTons) {
this.massTons = massTons;
}
}
}