Difference between Memento Pattern and Serialization

后端 未结 4 1270
醉酒成梦
醉酒成梦 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: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.

提交回复
热议问题