When persisting an object, you sometimes face the following problem: Some or all of the object's internal states are private. Therefore, there is no way to obtain the values of those states without violating encapsulation.
However, you might argue, the object serialization technique in the .NET Framework allows you to persist objects into a file stream or other device. Yes, that is true. Nonetheless, the classes of some objects do not implement the ISerializable interface or are not marked with the <Serializable> attribute. For example, you may want to persist an instance of a class that inherits from the System.Windows.Forms.Control class, as is the case in our drawing application. The System.Windows.Forms.Control class is not marked with the <Serializable> attribute, and it does not implement the ISerializable interface. Attempting to serialize an object of this class will throw an exception.
In addition, there are two other problems to solve. The first relates to efficiency. Suppose you have a large object that you want to persist, but only a small fraction of the states inside the objects need to be persisted. Serializing the whole object will require greater space than necessary, not to mention that it will take longer time to serialize it and later retrieve it.
The second problem has to do with security. You may want to protect some internal state of the object. In other words, you may want to persist some states but not some other states.
The aforementioned issues make the object serialization technique seem useless, doesn't it? Not really. You can still use the .NET Framework object serialization technique with the help of the Memento design pattern.
The idea of the Memento pattern is simple. In this pattern, the object whose state you want to persist is called the originator. To apply the pattern, you create another object called the memento. The memento object is an external object that will hold the states of the originator. Therefore, if you need to save the states of the variables a, b, c, and d from the originator, the Memento pattern will have the same variables a, b, c, and d. But, hold on a second. What if some of the states are private fields, which of course are not accessible from outside the originator? The solution to this problem is the beauty of this pattern. The originator will have two extra methods: One is called CreateMemento, and the other is SetMemento.
The CreateMemento method instantiates a memento object, copies all its states that need to be persisted to the memento object, and then returns the memento object. Therefore, to persist the originator, you call its CreateMemento to obtain a memento object and then serialize the memento object.
The SetMemento method gets the states back. After you deserialize the previously saved memento object, you call the SetMemento object of the originator by passing the memento object obtained from the deserialization.
Clever, isn't it?
In the project in this chapter, you will modify the Memento design pattern a bit. The CreateMemento is called GetMemento, and instead of providing the SetMemento method, you will use a constructor that accepts a memento object. You can then use this constructor to instantiate the object as well as to restore the previous states.