java - 使用可序列化或修改的单例?
问题描述
我在主屏幕上有Application
一个List
。Objects
单击其中一个时,我将用户转移到下一个Fragment
.
然后我有更多 3 个Fragments
正在设置(操作)对象字段。我的问题是,它是否正确,而不是实现Serializable
interface
并通过Object
使用 Bunde 我会做一种 Singleton。像这样的东西(我知道对于单例我必须将空构造函数设为私有)。
public class Father {
private static Father instance;
private String name;
private int age;
Father() {
}
public static Father getInstance(Father father) {
if (instance == null) {
instance = new Father();
if(father != null) {
instance = father;
}
}
return instance;
}
Father regularFather = new Father();
regularFather.setAge(35);
regularFather.setName("Danny");
Father regularFather2 = new Father();
regularFather2.setAge(44);
regularFather2.setName("Mike");
Father regularFather3 = new Father();
regularFather3.setAge(15);
regularFather3.setName("Tom");
Father.getInstance(regularFather2).setName("Mike32");
System.out.println("regularFather name is: " + regularFather.getName()
+ " \n regularFather2 name is: " + regularFather2.getName()
+ " \n regularFather3 name is: " + regularFather3.getName()
+ " \n Instance name is: " + Father.getInstance(null).getName()
);
我对其进行了测试,效果很好,但我的问题是,这样做是否正确,如果它会在未来引起问题怎么办?
解决方案
推荐阅读
- redux - Immutable.js:创建一个包含一个元素的列表
- javascript - 触发 Modal 以在 useEffect 挂钩内显示的问题
- javascript - AWS Lambda 函数:无法读取未定义的属性 'logs' 'numberOfPages' 'stream'。包问题或 AWS Lambda 问题?
- html - 如何使用 CSS 隐藏行中的内联元素溢出?
- mysql - 如何允许我的团队成员访问我的 AWS mySQL 数据库?
- python - 如何在数据框中选择 NaN 单元格?
- reactjs - 开玩笑测试未通过创建反应应用程序
- java - 是否保证来自 ExecutorService 的线程更新本地声明的并发 hashmap?
- list - ELM:当我单击按钮时,Onlick 按钮可将 HTML 消息中的特定字符串移植到 JS
- java - 如何在 java 8 中创建嵌套对象列表?