我希望在整个应用程序中维护MyModel类的单个实例。所以我创建了一个类似于:
的Singleton类public class MySingleton {
private MySingleton () {}
public MyModel getMyModel() {
return myModel;;
}
public void setEmotionRecord(EmotionRecord emotionRecord) {
this.emotionRecord = emotionRecord;
}
private static MySingleton instance;
private MyModel myModel = new MyModel();
public static synchronized MySingleton getInstance() {
if (instance == null) {
instance = new MySingleton ();
}
return instance;
}
}
MyModel是一个模型类,类似于:
public class MyModel {
int a;
String b;
private MyModel() {}
//....Getter Setter of a and b
}
我可以认为它是一个很好的Singleton类吗?