纪元模式与序列化之间的差异 [英] Difference between Memento Pattern and Serialization

查看:152
本文介绍了纪元模式与序列化之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些关于Memento模式的研究,而且我通常对行为模式很新,而且我的研究一直在变得很困惑。我一直困惑的主要事情之一是Memento Pattern和序列化之间的区别。



从我可以收集的两者都可以用来存储对象,并有他们以后再带回来,但是我还没有找到一个明确的答案,他们之间的关键区别是什么,也许我错过了我的研究中的一些东西,但我想知道是否有人可以揭示什么差异在这两个之间。



谢谢

解决方案

用于实现回滚/保存点支持。例如,我可能想要在某个时间点标记一个对象的状态,做一些工作,然后决定将该对象还原到标记的位置。



实现一个Memento模式可以使用序列化,这将包括将对象的内容保存为一个字节[]并保留在内存中或写入磁盘。当返回对象的内容将从序列化的副本重建时。



相反,我可以通过在内存中克隆对象并保留对副本的引用来实现Memento模式然后如果对象需要还原,则将状态复制回来。此方法不使用序列化。


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

解决方案

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.

这篇关于纪元模式与序列化之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆