美文网首页
单例模式序列化时注意!

单例模式序列化时注意!

作者: henry随笔 | 来源:发表于2018-04-22 23:18 被阅读0次

单例模式类实现Serializable接口后, 在序列化时, getInstance方法变的不可用, 所以单例模式序列化时请为该类添加readResolve方法, 否则序列化出来的对象不是单例的.

private Object readResolve() throws ObjectStreamException {
    return instance;
}

相关文章

网友评论

      本文标题:单例模式序列化时注意!

      本文链接:https://www.haomeiwen.com/subject/qsbhlftx.html