I was reading about singletons the other day and thought to implement them in a project of mine, but I didn't like the way it seemed to flow logically, so I created what I'd call a controller class to manage the state of the singleton object. I want to make sure that the logic checks out though, and that I'm not inadvertently spawning off additional instances.
//Controller for SaveSystem, allows the class to be created once for the
//duration of the application at the global level (believe this is called a
//singleton pattern)
public class SaveSystemContr {
private static SaveSystem saveSystemInstance;
public SaveSystem GetSaveSystemData() {
return saveSystemInstance;
}
public void SetSaveSystem(SaveSystem _saveSystem) {
if(saveSystemInstance!=null) {
saveSystemInstance=_saveSystem;
}
}
public static SaveSystem getSaveSystemInstance(final FirebaseAuth _auth, final LoadProjFragment _LFP) {
if(saveSystemInstance==null) {
saveSystemInstance = new SaveSystem(_auth, _LFP);
}
return saveSystemInstance;
}
public SaveSystemContr() {} //THE WAY IS SHUT!
}
Edit* I do not view this as a duplicate of the question referenced, as that was a typical/standard implementation of a singleton, and this uses a different model altogether by utilizing a controller to manage the state of the singleton.