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?