Let’s say I have a model, this model is a singleton so there’s only one instance. I pass this model to other classes and they keep a it in a variable.
Then later I replace this same model at it’s origin point with a serialized version of the model. The other classes that hold a variable of this model, they will still point to the old reference right?
How would I go to replace the model with a new reference and get all the other classes to point to this new one?
If these other objects all hold references to the singleton, you would need to manually update them by (re)calling methods passing in the new instance and re-setting the variables.
A smarter way would be to pass around a simple wrapper object which itself referenced the singleton, and which all other objects accessed it through. When you needed to update to the new singleton, you would only have to change the wrapper’s reference.
Typical singleton implementations simplify this even further by just publicly exposing a static
get()method on the singleton class, and leaving it up to other classes to call this method, instead of passing the singleton around like a visitor.