Recently,I am write about desgin pattern article,in single pattern, I use “object” to create single instancce . When I try to deserialize to get a new instance, I find that getting the instance or the previous one, and not creating a new one, how is this done I want to know.
First of all, don’t provide code samples as images. It makes impossible for readers to use the code easily.
Second, it really depends on the serialization library you use. Kotlin can’t control how parseObject
works internally. Some libraries could return the singleton object, some could return additional instances, some could fail in this case.
I suspect in your case this is the second scenario, so you don’t actually get the singleton, but another instance. Please check Single === newSingle
. It works because name
is static, so all instances reference the same value.