Momento - Harsh4999/Design-Patterns GitHub Wiki

Momento

USE

  • Whenever we want to store object's state without exposing internal details about the state then we can use momento design patterns.
  • like if we want to take a snapshot of our object's state which will help us in implementing undo feature.
  • Only object who can read or create snapshot is the object
  • Ask state as single sealed object

UML

  • Originator: Object who's state we want to store
  • Momento: Originator object acts as a stored state
  • Care Taker: holder for Momento's object

Implementation

  • Find out which state needs to be stored in momento
  • We then implement the momento with reqwuirement that it can't be changed & read outside the originator
  • Momento should not expose anything
  • Only originator can read momento
  • Originator should have a method which get current snapshot and return instance of momento
  • Another method in originator takes a momento object as args and the originator resets to momento's state