Effective Java Item #77 - Serialization of singleton objects - Why should I have to use readResolve?

后端 未结 3 898
刺人心
刺人心 2020-12-10 23:15

Effective Java #77 states that we have to use readResolve to preserve the Singleton guarentee during serialization. They have used the example.

         


        
3条回答
  •  轮回少年
    2020-12-10 23:48

    I'd suggest creating a proxy class inside your singleton class to hold all of your variables. Then you can have accessor functions [getProxyClass() & setProxyClass()]. Make the proxy class serializable, then when you go to serialize or deserialize use the proxy class and just use the accessor functions to get or set it. If you do it this way it cuts out a lot of the mess involved with the singleton class.

提交回复
热议问题