有没有一种单例设计可以完全防止创建多个对象?
问这个问题时请记住,在 Java 中,我们可以使用序列化-反序列化和反射来重新创建“单例对象”。
最佳答案
是的,我们可以设计一个真正的单例类(也是线程安全的)。
但每个单例类只是给定类加载器中的单例,
运行时不在整个 JVM 内。
有关更多详细信息,请查看这些相关问题。
What is an efficient way to implement a singleton pattern in Java?
How does Singleton behave when two threads call the "getInstance()" at the same time?
How to correctly make a thread safe Singleton Factory in Java?
关于java - 我们能用java设计一个真正的单例类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21533368/