源码中的设计模式-单例模式

jdk源码

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() {}

...
}

  JDK 中,java.lang.Runtime 就是经典的单例模式(饿汉式)

单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对一些需要频繁创建销毁/耗时/耗费资源的对象(比如工具类、频繁访问数据库或文件的对象),单例模式可以提高系统性能。

收住自己的心 一步一个脚印 做好自己的事
原文地址:https://www.cnblogs.com/GodMode/p/13764939.html