先日シングルトンについて読んでいて、自分のプロジェクトに実装しようと思ったのですが、論理的に流れているように見えるのが気に入らなかったので、コントローラークラスと呼ばれるものを作成して、状態を管理しましたシングルトン オブジェクト。ただし、ロジックがチェックアウトされていること、および誤って追加のインスタンスを生成していないことを確認したいと考えています。
//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!
}
編集*これは、参照されている質問の複製とは見なされません。これは、シングルトンの典型的/標準的な実装であり、コントローラーを使用してシングルトンの状態を管理することにより、まったく異なるモデルを使用します。