4

Imagine for a second that I'm implementing the Memento Pattern, using the following classes.

Classes

  • An Originator class that has public and private, properties and fields respectively
  • A Memento class that has many properties/fields representing state of Originator
  • A CareTaker class that acts on an instance of Originator, and saves a list of Mementos representing the Originator's state.

Using the example classes above, is there an objective reason to use one of the following solutions over the other? The premise here is that I need to expose a few properties that partially represent the state of the Originator.

Solution #1

  • Properties and fields representing state are duplicated across Originator & Memento
  • Originator has a CreateMemento method that initializes a new Memento with all of the shared properties/fields between the two
  • Originator has a SetMemento method that sets Originator's properties/fields to those that correspond with Memento's

Solution #2

  • Originator has an instance of Memento, representing Originator's current state
  • Originator exposes as properties, the necessary properties/fields of it's Memento, using the getters/setters to update the memento
  • Originator doesn't have it's own copy of properties/fields representing its state, instead it uses it's Memento as a backing store.

I understand that ideally there should be yet another class OriginatorState, that Originator & Memento both have instances of, but I'm not asking about which classes to create. I'm asking which class holds the information of the current state of the Originator. Both the Originator and the Memento? Just the Memento? or the Originator vicariously through the Memento as in Solution #2?

1

Once the Originator generates a Memento, the Caretaker shouldn't have an API available to modify the data contained within that object. The key is that encapsulation isn't lost when giving the Memento to the Caretaker.

Objectively, if the only really difference between Option 1 and Option 2 is that the Originator uses an instance of the Memento internally to maintain its state, it really doesn't matter. It is not required to, but it is not prohibited.

-1

From the description of the Memento Design Pattern:

The Memento design pattern defines three distinct roles:

  1. Originator - the object that knows how to save itself.

  2. Caretaker - the object that knows why and when the Originator needs to save and restore itself.

  3. Memento - the lock box that is written and read by the Originator, and shepherded by the Caretaker.

The Originator is responsible for creating the Memento and restoring itself to a previous state. A Memento is just a bag of data devoid of logic. The CareTaker knows when to get a Memento and when the Originator should be restored from a Memento.

  • In both solutions the Originator creates the Momento. Restoration and CareTaker's aforementioned knowledge weren't listed in either solution but could be expected to follow your suggestion in both solutions. Also you haven't answered the question as to which solution is better. If you had answered the question, your reasoning would still be subjective, as it's the opinion of sourcemaking.com that those are each contributors roles. – D-Jones Sep 4 '15 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.