单例控制器 Java
Singleton Controller Java
前几天我在阅读有关单例的文章,并想在我的一个项目中实现它们,但我不喜欢它看起来符合逻辑的方式,所以我创建了我称之为控制器的东西class来管理单例对象的状态。不过,我想确保检查逻辑,并且我不会无意中产生额外的实例。
//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!
}
编辑* 我不认为这是所引用问题的重复,因为那是单例的 typical/standard 实现,并且它通过利用控制器来管理状态来使用完全不同的模型单身人士。
I want to make sure that the logic checks out though, and that I'm not inadvertently spawning off additional instances.
看来您可以创建任意数量的实例:
SaveSystemContr controller = new SaveSystemContr();
// Create instance 1
controller.SetSaveSystem(new SaveSystem(auth, lfp));
// Create instance 2
controller.SetSaveSystem(new SaveSystem(auth, lfp));
// ...
如果您只需要 1 个实例,为什么要使用 setter 方法?
一个非常基本的单例将如下所示:
public final class SaveSystemSingleton {
// This class should only be accessed statically. Don't allow instance creation
private SaveSystemSingelton() {}
private static SaveSystem saveSystem;
// Encapsulate creation logic. Passing in params is misleading because
// all subsequent calls will do nothing with the params.
public static SaveSystem get() {
// If accessing from multiple threads do Double Check Locking instead!
if (saveSystem == null) {
saveSystem = new SaveSystem(new FirebaseAuth(), new LoadProjFragment());
}
return saveSystem;
}
}
如果确实需要传入params,定义一个单独的静态setter方法,如果多次调用或者没有先调用get()
就抛出异常setter.
此外,您应该检查依赖项注入(例如 Dagger2),这使得创建对象实例和确定它们的范围变得清晰和容易。
将构造函数设为私有并删除 getter 和 setter :
//Allows the OBJECT (not class) to be created once only
public class SaveSystem {
private static SaveSystem saveSystemInstance;
//make constructor private
private SaveSystem(final FirebaseAuth _auth, final LoadProjFragment _LFP) {
//todo complete constructor
}
public static SaveSystem getSaveSystemInstance(final FirebaseAuth _auth, final LoadProjFragment _LFP) {
if(saveSystemInstance==null) {
saveSystemInstance = new SaveSystem(_auth, _LFP);
}
return saveSystemInstance;
}
}
如您所见,它可以在没有控制器的情况下应用于 SaveSystem
。
前几天我在阅读有关单例的文章,并想在我的一个项目中实现它们,但我不喜欢它看起来符合逻辑的方式,所以我创建了我称之为控制器的东西class来管理单例对象的状态。不过,我想确保检查逻辑,并且我不会无意中产生额外的实例。
//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!
}
编辑* 我不认为这是所引用问题的重复,因为那是单例的 typical/standard 实现,并且它通过利用控制器来管理状态来使用完全不同的模型单身人士。
I want to make sure that the logic checks out though, and that I'm not inadvertently spawning off additional instances.
看来您可以创建任意数量的实例:
SaveSystemContr controller = new SaveSystemContr();
// Create instance 1
controller.SetSaveSystem(new SaveSystem(auth, lfp));
// Create instance 2
controller.SetSaveSystem(new SaveSystem(auth, lfp));
// ...
如果您只需要 1 个实例,为什么要使用 setter 方法?
一个非常基本的单例将如下所示:
public final class SaveSystemSingleton {
// This class should only be accessed statically. Don't allow instance creation
private SaveSystemSingelton() {}
private static SaveSystem saveSystem;
// Encapsulate creation logic. Passing in params is misleading because
// all subsequent calls will do nothing with the params.
public static SaveSystem get() {
// If accessing from multiple threads do Double Check Locking instead!
if (saveSystem == null) {
saveSystem = new SaveSystem(new FirebaseAuth(), new LoadProjFragment());
}
return saveSystem;
}
}
如果确实需要传入params,定义一个单独的静态setter方法,如果多次调用或者没有先调用get()
就抛出异常setter.
此外,您应该检查依赖项注入(例如 Dagger2),这使得创建对象实例和确定它们的范围变得清晰和容易。
将构造函数设为私有并删除 getter 和 setter :
//Allows the OBJECT (not class) to be created once only
public class SaveSystem {
private static SaveSystem saveSystemInstance;
//make constructor private
private SaveSystem(final FirebaseAuth _auth, final LoadProjFragment _LFP) {
//todo complete constructor
}
public static SaveSystem getSaveSystemInstance(final FirebaseAuth _auth, final LoadProjFragment _LFP) {
if(saveSystemInstance==null) {
saveSystemInstance = new SaveSystem(_auth, _LFP);
}
return saveSystemInstance;
}
}
如您所见,它可以在没有控制器的情况下应用于 SaveSystem
。