是一个只有一个实例的类,如果它没有'没有全球范围

is class with only one instance considered a singleton if it doesn't have global scope?

本文关键字:如果 范围 一个 实例 有一个      更新时间:2023-09-26

我读到关于单重态的文章,到处都提到单重态不好,因为它们具有全局作用域。如果我有一个只有一个实例的类,但不能全局访问它,那么它应该作为依赖项注入——这个类仍然是单例吗?

我已经找到了我自己问题的答案——引用这里的话:

When we say “Singleton” or “JVM Singleton” in this document, we mean the classic Gang of Four singleton. (We say that this singleton enforces its own “singletonness” through a static instance field). An “application singleton” on the other hand is an object which has a single instance in our application, but which does not enforce its own “singletonness.”

所以基本上它们区分了两种不同类型的单例类。