1 / 7

Understanding the Memento Design Pattern

Learn about the Memento pattern, a design pattern that captures and restores an object's internal state. This pattern is helpful when you need to save a snapshot of an object's state for future restoration without exposing implementation details. Explore the participants involved in the Memento pattern - Originator, Memento, and Caretaker - and see an example with a Security Guard, Owner, and Safety Deposit scenario. Dive into the UML diagram and see how to implement the Memento pattern in your own code.

latch
Download Presentation

Understanding the Memento Design Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memento Kendra Kachelein Maureen Thomas

  2. Definition • The memento captures and externalizes an object’s internal state, so the object can be restored to that state later.

  3. Applicability • Use the Memento pattern when: • A snapshot of an object’s state must be saved so that it can be restore to that state later, and • A direct interface to obtaining the state would expose implementation details and break the object’s encapsulation

  4. Participants • There are 3 participants in the Memento pattern: • Originator: whose state has to be maintained • Memento: stores the state of the Originator object • Caretaker: has a handle to the Memento

  5. Example 34-55-09 Caretaker Memento Originator

  6. UML Diagram Caretaker Originator Memento -State -State +GetState() +SetState() +SetMemento(in Memento) +CreateMemento() Class Example: Security Guard Owner Safety Deposit -State -State +GetCombo() +GiveCombo() +DepositCombination(in Memento) +WriteCombination()

  7. Source Code Implementation public class Owner { private int combination public safetyDeposit getCombo() { return new saftetyDeposit(this); } public void giveCombo(safetyDeposit sd) { combination = sd.combination; }

More Related