Difference between Memento Pattern and Serialization
Asked Answered
S

4

17

I am doing some research into the Memento Pattern and I am generally new to behavioural patterns and with my research I have been getting pretty confused. One of the main things I have been getting confused on is the differences between the Memento Pattern and Serialization.

From what I can gather both can be used to store objects and have them brought back at a later date but I have not been able to find a clear cut answer on what the key differences between them are, maybe I have missed something in my research but I was wondering if anyone could shed some light on what the differences are between the two.

Thanks

Significative answered 28/12, 2012 at 22:50 Comment(0)
H
20

Typically the Memento pattern is used to implement roll-back/save point support. For example I might want to mark the state of an object at a point in time, do some work and then decide to revert that object back to the point at which is was marked.

The implementation of a Memento pattern could use serialisation, which would involve saving the contents of the object into a byte[] and keeping in memory or writing to disk. When reverting the content of the object would be rebuilt from the serialised copy.

Conversely I could implement a Memento pattern by cloning the object in memory and keeping a reference to the copy and then copying the state back if the object needs reverting. This method doesn't use serialisation.

Hollenbeck answered 28/12, 2012 at 22:54 Comment(5)
Ah understood, from what I can gather most examples of the Memento Pattern seem to use the object cloning approach. Is there any particular reason why most examples use object cloning over Serialization?Significative
Because cloning an object is a lighter process than serialization. Suppose you have an object Foo with a String inside, of 1000 characters. Cloning it will just create a new object with a references to the same String instance (only a few bytes needed), and the result will be of type Foo. If you serialize the object, you will have a result of type byte[], that will contain the whole content of the two strings (5 or 6 KBs), and when restoring the object, you'll have to deserialize it, and create a new copy of the two strings. It could be used to implement a deep clone, it(susually not needed.Davidoff
Just a note here that both cloning and serialization can be fairly opaque operations. I'm never very comfortable serializing an object when it has a lot of complex references to other objects. Additionally, there are objects that don't serialize at all, such as Threads, Network Sockets.Nabila
thanks for showing diff ways of saving and restoring -- cloning and serialization.Petua
Here is a discussion related to Storing a memento/state in a databaseLilly
D
8

The Memento pattern is an OO design pattern used to keep previous states of an object in memory. It's useful to implement an "Undo" operation, for example.

Serialization is the process of transforming a graph of objects to a byte array, in order to save it on disk, or send it to another JVM over the network, for example. They don't have much in common.

Davidoff answered 28/12, 2012 at 22:55 Comment(0)
C
2

Memento is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).

Strucutre of memento:

enter image description here

The memento pattern is implemented with three objects: the originator, a caretaker and a memento.

The originator is some object that has an internal state.

The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.

Serialization is used to persist the object state. It's not a design pattern. Refer to this SE question for more details on Serialization.

Use of Serializable other than Writing& Reading object to/from File

Memento pattern may or may not use Serialization. If memento object is not leaving JVM or not passed to other services over remote calls, memento can store object state in memory with out Serialization. The stored object can be used later to change the state.

Refer to sourcemaking article for further details.

Cords answered 15/7, 2016 at 5:44 Comment(0)
N
1

Design patterns as the name implies address Design issues.

Serialization is a way to "freeze dry" an object.

So Serialization could be an implementation mechanism by which you could implement the Memento Pattern.

However you could just as easily implement the memento pattern without using serialization.

Nabila answered 28/12, 2012 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.