Search code examples
design-patternsmemento

How to create default instances of the memento in a Memento Pattern?


I use the Memento Pattern to save properties of a multi-instance form, where n forms are created by user inside a parent form. The purpose of the memento is to regain the same number of forms, and their settings, when a user close and later reopen the parent form. The saving of the form mementos are done by a "save" button on the parent form. So I have two challenges..

  1. I need a default set of properties to use when opening a new form, so where do I set these default values.. In a default empty constructor of the memento, or somewhere (where?) in the Originator?
  2. Next I want the user to be able to change the defaults, hence to make their own default memento. How/where is this saved, and how do I determine if such default memento has been set or not?

Of course I could probably tweak this to work somehow, but I would like to know if there is a general pattern description that solves this, such that I have a firm guideline to follow.

cheers!


Solution

  • The default empty constructor of the memento class should be fine, or you can have a CreateDefault() factory method, e.g., if you want to leave the default constructor to initialize a blank memento as opposed to a pre-set default configuration.

    There are a number of different creational patterns you can use for specifying a custom default, including prototype and abstract factory.