【发布时间】:2016-12-07 13:01:02
【问题描述】:
我的 Android 应用程序中有一个配置类,我存储在 Realm 数据库中。我只是对其进行了编辑以遵循单例设计模式,因此配置实例只有一次。
问题是,该类现在有一个私有构造函数来防止实例化,而无需调用我的“getInstance”方法。但是,RealmObject 似乎需要一个公共构造函数。
难道不能有一个使用单例设计模式扩展 RealmObject 的类吗?
这仅仅是我必须考虑的 Realm 限制吗?
这是上下文的 sn-p 代码:
public static AppConfiguration getInstance(){
if(configuration == null){
synchronized (AppConfiguration.class) {
if (configuration == null) {
configuration = new AppConfiguration();
}
}
}
return configuration;
}
//constructor is private to prevent instantiation without using getInstance, enforces singleton
private AppConfiguration() { //The constructor defined as private which is causing the problem
this.isRegistered = false;
this.isLoggedIn = false;
}
【问题讨论】:
标签: java android constructor singleton realm