Suggest an editImprove this articleRefine the answer for “What three roles are involved in Memento?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The **Memento** pattern involves **three key roles**, each responsible for its own aspect of saving and restoring state. **Key point:** **Originator** is responsible for the data, **Memento** for safe storage, **Caretaker** for controlling the history, while preserving encapsulation and the independence of components.Shown above the full answer for quick recall.Answer (EN)ImageThe **Memento** pattern involves **three key roles**, each responsible for its own aspect of saving and restoring state: --- ### 1. **Originator** The object whose state needs to be saved and later restored. It knows for itself which data is important to capture, and it can: - create a snapshot (`createMemento()`), - restore itself from one (`restore(memento)`). **Example:** ```java class Editor { private String text; public void setText(String text) { this.text = text; } public String getText() { return text; } public Memento save() { return new Memento(text); } public void restore(Memento m) { text = m.getState(); } } ``` The Originator is the **owner of the state** and the **only one who knows its structure**. --- ### 2. **Memento** The object that **holds the Originator's state** at a particular point in time. Its internals are **closed off** from other objects, no one but the Originator can change or read the state. **Example:** ```java class Memento { private final String state; public Memento(String state) { this.state = state; } public String getState() { return state; } } ``` The Memento is an **encapsulated container** for the state. --- ### 3. **Caretaker** The object that **manages the history of snapshots**, but doesn't know what's inside them. It saves, retrieves, and passes snapshots back to the Originator when needed. **Example:** ```java class History { private Stack<Memento> history = new Stack<>(); public void push(Memento m) { history.push(m); } public Memento pop() { return history.pop(); } } ``` The Caretaker is responsible for the **lifecycle of snapshots**, but doesn't interfere with their contents. --- ### **In Short** | Role | Responsibility | |---|---| | **Originator** | Creates and restores its own state | | **Memento** | Stores the state without exposing implementation details | | **Caretaker** | Stores and manages snapshots without knowing their contents | --- ### **Conclusion** The **Memento** pattern splits responsibilities so that: - **Originator** is responsible for the data, - **Memento** for safe storage, - **Caretaker** for controlling the history, while preserving **encapsulation and the independence of components**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.