按需初始化持有者 Idiom 如何确保只有“单个”对象?实例被创建过吗?
我浏览了维基 https://en.wikipedia.org/wiki/Initialization-on -demand_holder_idiom 来理解 Singleton 模式的按需持有者初始化习惯用法,尽管 wiki 提到了它如何是线程安全的,但它并没有说明这实际上如何确保只有一个实例创建过,即如果在两个不同的类中调用 Something.getInstance()
他们如何拥有相同的对象?
我是 Java 新手,所以任何帮助将不胜感激。
I went through the wiki https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom to understand Initialization on demand holder Idiom for Singleton pattern, though the wiki mentions how it's thread safe, it doesn't say how this actually ensures only one instance is created ever, i.e if in two different classes Something.getInstance()
is called how do they have the same object ?
I am new to Java, so any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单例的基本模式:
Basic pattern for a singleton: