Java API 中单例设计模式的最佳示例是什么? Runtime
类是单例吗?
最佳答案
只有两个例子浮现在脑海中:
另见:
更新:回答 PeterMmm(目前已删除?)的评论(询问我如何知道它是单例),检查 javadoc 和源代码:
public class Runtime {
private static Runtime currentRuntime = new Runtime();
/**
* Returns the runtime object associated with the current Java application.
* Most of the methods of class <code>Runtime</code> are instance
* methods and must be invoked with respect to the current runtime object.
*
* @return the <code>Runtime</code> object associated with the current
* Java application.
*/
public static Runtime getRuntime() {
return currentRuntime;
}
/** Don't let anyone else instantiate this class */
private Runtime() {}
它每次都返回相同的实例,并且它有一个private
构造函数。
关于java - Java API 中单例类的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3061328/