mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-03 04:54:39 +00:00
docs: update memento
This commit is contained in:
1 parent
df578ce58d
commit
1b04adadb8
3 files changed
+41
-42
No files matched your search
+35
-17
@@ -3,34 +3,36 @@ title: Memento
|
|||||||
category: Behavioral
|
category: Behavioral
|
||||||
language: en
|
language: en
|
||||||
tag:
|
tag:
|
||||||
- Gang of Four
|
- Encapsulation
|
||||||
|
- Gang of Four
|
||||||
|
- Memory management
|
||||||
|
- Object composition
|
||||||
|
- State tracking
|
||||||
|
- Undo
|
||||||
---
|
---
|
||||||
|
|
||||||
## Also known as
|
## Also known as
|
||||||
|
|
||||||
Token
|
* Snapshot
|
||||||
|
* Token
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
Without violating encapsulation, capture and externalize an object's internal state so that the
|
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
|
||||||
object can be restored to this state later.
|
|
||||||
|
|
||||||
## Explanation
|
## Explanation
|
||||||
|
|
||||||
Real-world example
|
Real-world example
|
||||||
|
|
||||||
> We are working on an astrology application where we need to analyze star properties over time. We
|
> 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.
|
||||||
> are creating snapshots of star states using the Memento pattern.
|
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
> Memento pattern captures object internal state making it easy to store and restore objects in any
|
> Memento pattern captures object internal state making it easy to store and restore objects in any point of time.
|
||||||
> point of time.
|
|
||||||
|
|
||||||
Wikipedia says
|
Wikipedia says
|
||||||
|
|
||||||
> The memento pattern is a software design pattern that provides the ability to restore an object to
|
> The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
|
||||||
> its previous state (undo via rollback).
|
|
||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
@@ -43,12 +45,11 @@ public enum StarType {
|
|||||||
WHITE_DWARF("white dwarf"),
|
WHITE_DWARF("white dwarf"),
|
||||||
SUPERNOVA("supernova"),
|
SUPERNOVA("supernova"),
|
||||||
DEAD("dead star");
|
DEAD("dead star");
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Next, let's jump straight to the essentials. Here's the `Star` class along with the mementos that we
|
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.
|
||||||
need to manipulate. Especially pay attention to `getMemento` and `setMemento` methods.
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public interface StarMemento {
|
public interface StarMemento {
|
||||||
@@ -110,7 +111,7 @@ public class Star {
|
|||||||
private int massTons;
|
private int massTons;
|
||||||
|
|
||||||
// setters and getters ->
|
// setters and getters ->
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -162,12 +163,29 @@ sun age: 10000000 years mass: 500000 tons
|
|||||||
Use the Memento pattern when
|
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 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
|
* A direct interface to obtaining the state would expose implementation details and break the object's encapsulation
|
||||||
object's encapsulation
|
|
||||||
|
|
||||||
## Known uses
|
## 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
|
## Credits
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public class App {
|
|||||||
states.add(star.getMemento());
|
states.add(star.getMemento());
|
||||||
star.timePasses();
|
star.timePasses();
|
||||||
LOGGER.info(star.toString());
|
LOGGER.info(star.toString());
|
||||||
while (states.size() > 0) {
|
while (!states.isEmpty()) {
|
||||||
star.setMemento(states.pop());
|
star.setMemento(states.pop());
|
||||||
LOGGER.info(star.toString());
|
LOGGER.info(star.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.memento;
|
package com.iluwatar.memento;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Star uses "mementos" to store and restore state.
|
* Star uses "mementos" to store and restore state.
|
||||||
*/
|
*/
|
||||||
@@ -85,34 +88,12 @@ public class Star {
|
|||||||
/**
|
/**
|
||||||
* StarMemento implementation.
|
* StarMemento implementation.
|
||||||
*/
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
private static class StarMementoInternal implements StarMemento {
|
private static class StarMementoInternal implements StarMemento {
|
||||||
|
|
||||||
private StarType type;
|
private StarType type;
|
||||||
private int ageYears;
|
private int ageYears;
|
||||||
private int massTons;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user