views:

42

answers:

2

Hi, 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!

+1  A: 

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.

Mark Cidade
Thanks Mark! I'm trying to understand the patterns you suggest. Takes a bit time, as I'm quite new to this field.. :)
bretddog
+1  A: 

Your description does not really match with the Memento patttern. The whole point of Memento is that only instances of the class which is to be restored know anything about the representation of the memento. That is, Memento is about hiding state, rather than allowing clients to set arbitrary states.

As Mark Cidade suggests above, there are other more appropriate patterns to use for your problem.

Burleigh Bear
Thanks Bear! I've been thinking about Mark's response, and trying to study the prototype pattern. As you understand I'm not very educated on pattern design. I thought Mark meant to combine them, but maybe I misunderstood.. "The whole point of Memento is that only instances of the class which is to be restored know anything about the representation of the memento."; My main form opens a file with a list of mementos, then opens one new child form for each of the mementos, sending the memento as a constructor parameter.. Does this still sound like improper use of the pattern?
bretddog
..And the Originator is a part of the Child Form Class. Indeed I made two levels of Originators.. So I have memento for the Child Form Class, and also a sub-memento for a UserControl.. So the Child Form memento contains a memento for a UserControl.. if that makes sense.. :)
bretddog
This is because I want to reuse/duplicate the settings of a UserControl, across several Child Forms. So I can for example just click on the UserControl context menu, "copy template to all", and it will raise an event to the main form, that sends out this UserControl memento to all child forms.. Still if I can do the same with a better pattern I would be very interested to learn that..
bretddog