VOOZH about

URL: https://www.javacodegeeks.com/2020/11/memento-pattern.html

โ‡ฑ Memento Pattern - Java Code Geeks


Without violating encapsulation, capture and externalize an objectโ€™s internal state so that the object can be restored to this state later.

๐Ÿ‘ Image

Participants

  • Memento: stores internal state of the Originator object. The memento may store as much or as little of the originatorโ€™s internal state as necessary at its originatorโ€™s discretion. Protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface to the Memento โ€” it can only pass the memento to the other objects. Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator that produces the memento would be permitted to access the mementoโ€™s internal state.
  • Originator: creates a memento containing a snapshot of its current internal state. Uses the memento to restore its internal state
  • Caretaker: is responsible for the mementoโ€™s safekeeping. Never operates on or examines the contents of a memento.

Code

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class Main {
    public static void main(String[] args) {
        Originator o = new Originator();
        o.setState("On");
        Caretaker c = new Caretaker();
        c.setMemento(o.createMemento());
        o.setState("Off");
        o.setMemento(c.getMemento());
    }
}
public class Originator {
    private String state;
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
        System.out.println("State = " + state);
    }
    public Memento createMemento() {
        return new Memento(state);
    }
    public void setMemento(Memento memento) {
        System.out.println("Restoring state...");
        setState(memento.getState());
    }
}
public class Memento {
    private String state;
    public Memento(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}
public class Caretaker {
    private Memento memento;
    public Memento getMemento() {
        return memento;
    }
    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}

Output

1
2
3
4
State = On
State = Off
Restoring state...
State = On

eidherjulian61 / design-patterns

Published on Java Code Geeks with permission by Eidher Julian, partner at our JCG program. See the original article here: Memento Pattern

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

๐Ÿ‘ Photo of Eidher Julian
Eidher Julian
November 2nd, 2020Last Updated: October 30th, 2020
2 232 2 minutes read

Eidher Julian

Eidher Julian is a Systems Engineer and Software Engineering Specialist with 13+ years of experience as a Java developer. He is an Oracle Certified Associate and SOA Certified Architect.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
ruurd
5 years ago

A nice variation is to use a stack to push mementos on, that way you can create an Undo mechanism in your application.

2
Reply
5 years ago
Reply to  ruurd

Youโ€™re right. Thanks for your comment

0
Reply
Back to top button
Close
wpDiscuz