Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

2 Memento: Basic Idea Sometimes it is necessary to record the internal state of an object; e.g., when implementing checkpoints and undo mechanisms. You must save the state information somewhere so that you can restore objects to their previous state. But objects normally encapsulate some or all of their state, making it inaccessible to other objects and impossible to save externally. A memento is an object that stores a snapshot of the internal state of another object, the memento’s originator. ©SoftMoore ConsultingSlide 2

3 Memento: Basic Idea (continued) The originator initializes the memento with information that characterizes its current state. Only the originator can store and retrieve information from the memento – the memento is “opaque” to other objects. ©SoftMoore ConsultingSlide 3

4 Memento Pattern Intent: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later. Also Known As: Token Slide 4©SoftMoore Consulting

5 Memento Pattern (continued) Applicability: Use the Memento pattern when a snapshot of some portion of an object’s state must be saved so that it can be restored to that state later, and a direct interface to obtaining the state would expose implementation details, and therefore would break the object’s encapsulation. ©SoftMoore ConsultingSlide 5

6 Memento Pattern (continued) ©SoftMoore ConsultingSlide 6 Caretaker Structure Originator state setMemento(Memento m) createMemento() state = m.getState() Memento state getState() setState() return new Memento(state) memento

7 Memento Pattern (continued) Participants Memento –stores internal state of the Originator object. –protects against access by objects other than the originator. narrow interface for Caretaker (can only pass the memento to other objects wide interface for Originator (lets the originator access all data necessary to restore itself to its previous state) Originator –creates a memento containing a snapshot of its current internal state. –uses the memento to restore its state. ©SoftMoore ConsultingSlide 7

8 Memento Pattern (continued) Participants (continued) Caretaker –is responsible for the memento’s safekeeping. –never operates on or examines the contents of a memento. ©SoftMoore ConsultingSlide 8

9 Memento Pattern (continued) Collaborations A caretaker requests a memento from an originator, holds it for a time, and passes it back to the originator ©SoftMoore ConsultingSlide 9 m : Memento: Caretaker createMemento() : Originator setMemento(m) «create» setState() getState()

10 Memento Pattern (continued) Consequences: The Memento pattern Preserves encapsulation boundaries. Memento avoids exposing information that only an originator should manage but that must be stored outside the originator. Simplifies Originator. Moves the storage management burden from the Originator to the clients. Having clients manage the state they ask for simplifies Originator. Can be expensive to use. Memento can incur considerable overhead if the originator must copy large amounts of information or if clients create/return mementos to the originator frequently. ©SoftMoore ConsultingSlide 10

11 Memento Pattern (continued) Consequences (continued) Defines narrow and wide interfaces. It may be difficult in some languages to ensure that only the originator can access the memento’s state. Can have hidden costs. A caretaker can incur large storage costs when it stores mementos. ©SoftMoore ConsultingSlide 11

12 Memento Pattern (continued) Implementation Language support. Mementos have two interfaces, a wide one for originators and a narrow one for other objects. C++ supports this requirement by letting you make Originator a friend of Memento and making Memento’s wide interface private. Only the narrow interface should be declared public. Storing incremental changes. The memento can save just the incremental change to the originator’s internal state rather than the full state of every originator object. ©SoftMoore ConsultingSlide 12

13 Memento Pattern in Java Objects that implement interface S erializable (from package java.io ) can be viewed as examples of the Memento pattern. –allows objects to be written to a stream –can be used for persistence (writing to a file stream) or for socket communication Serializable is an empty interface – just a marker. Classes that implement the interface are marked as serializable. ©SoftMoore ConsultingSlide 13

14 ©SoftMoore ConsultingSlide 14 Java Serialization/Deserialization Serialization: write object as a sequence of bytes to a stream Deserialization: recreate brand new object on the other end with the original object’s data Note: Deserialization does not call the default constructor. It simply creates a blank object and fills in the fields with values retrieved via deserialization.

15 ©SoftMoore ConsultingSlide 15 Serialization (continued) Object Serialization Java Application Class A instance int x = 4 B b = Class B instance Serialized Object(s) in Arbitrary File Java Application Class A instance int x = 4 B b = Class B instance DeserializationSerialization JVM External Storage

16 Memento Pattern in Android Android provides a “Memento-like” class called a Bundle that is used in conjunction with lifecycle methods to save/restore the state of an activity. Under low memory conditions, any activity not in the foreground can be killed. When an activity is about to be killed, the Android runtime calls the activity’s onSaveInstanceState() method, passing a Bundle parameter that can be used to record state information that needs to be saved. When an activity is recreated, the Android runtime calls the activity’s onRestoreInstanceState() method, passing it the previously saved Bundle object. ©SoftMoore ConsultingSlide 16

17 Related Patterns Commands can use mementos to maintain state for undoable operations. Mementos can be used to represent the current state of an iteration. ©SoftMoore ConsultingSlide 17

18 References Memento pattern (Wikipedia) https://en.wikipedia.org/wiki/Memento_pattern Memento Pattern (Object-Oriented Design) http://www.oodesign.com/memento-pattern.html Memento Design Pattern (SourceMaking) https://sourcemaking.com/design_patterns/memento Memento (dofactory) http://www.dofactory.com/net/memento-design-pattern ©SoftMoore ConsultingSlide 18


Download ppt "The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google