枚举,单身和反序列化 [英] Enums, Singletons and Deserialization

查看:147
本文介绍了枚举,单身和反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枚举被认为是单身人士的最佳方法,其中一个原因是它隐式地继承可序列化



但是枚举如何防止单例的反序列化问题?

解决方案

序列化机制在特殊的具体方式。但传统的单例可以通过定义返回唯一实例的 readResolve()方法来反序列化。有关示例,请参阅 http://www.oodesign.com/singleton-pattern.html


Enums are considered best way for singletons and one of reasons for this is that it implicitly inherits Serializable.

But how enums prevents de-serialization problem of singletons?

解决方案

The serialization mechanism handles them in a special, specific way. But traditional singletons can be deserialized fine by defining a readResolve() method that returns the unique instance. See http://www.oodesign.com/singleton-pattern.html for an example.

这篇关于枚举,单身和反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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