Difference between Memento Pattern and Serialization

我是研究僧i 提交于 2020-01-10 08:55:38

问题


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


回答1:


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.




回答2:


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.




回答3:


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

Strucutre of memento:

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.




回答4:


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.



来源:https://stackoverflow.com/questions/14076772/difference-between-memento-pattern-and-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!