The Memento

Intent: Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later (Gamma et al., 1994).

Example: Sometimes it is necessary to take a point-in-time “snapshot” of an object so that it can be temporarily changed and then later restored to the original condition. This means that the state of the object must be able to be recorded and then restored. If the state in question is encapsulated (a best practice), then this would seem to be a challenge. How can we extract that which cannot be read? How can we restore that which cannot be written? The memento solves this problem.

The stateful object in question is typically called “the originator.” The object that captures the state is the memento. The memento must have a more intimate relationship to the originator than the rest of the system. There are many ways to accomplish this. In the example below, a narrow interface is used. Other implementations are possible depending on the technology being used.

images

Figure 11: Memento example diagram.

Qualities and Principles: The memento allows us to preserve the encapsulation of state, even under circumstances that would seem to dictate breaking it. The memento object is strongly cohesive in that it only stores state, nothing else. The interface of the originator is derived from the client's need to take a snapshot of the state. The ConcreteMemento (in the example) is substitutable for the memento that the client is exposed to, allowing the interface to vary in a way that is hidden to all clients.

Testing: The memento is an enabler for testing in that it allows a test to take an object through various scenarios and then return it to its original condition.

Testing that the memento accurately restores the state can be accomplished by making the test the originator, or by making it a subclass of the originator. The latter approach requires the state to be protected rather than private.

Questions and Concerns: The memento in the example does not literally enforce encapsulation in that a client object could, theoretically, downcast the reference in the same way that the originator does. If this is a concern, then the ConcreteMemento may be made package-private, or may be an inner class of the originator if the technology supports this idiom. Other possibilities exist, including the use of delegates or assembly metadata in .Net, etc.

For more information: https://tinyurl.com/y5hc4yo6

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset