Difference between Memento Pattern and Serialization

后端 未结 4 1164
醉酒成梦
醉酒成梦 2020-12-29 08:52

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 th

相关标签:
4条回答
  • 2020-12-29 09:01

    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.

    0 讨论(0)
  • 2020-12-29 09:04

    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.

    0 讨论(0)
  • 2020-12-29 09:06

    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.

    0 讨论(0)
  • 2020-12-29 09:14

    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.

    0 讨论(0)
提交回复
热议问题