单例模式
单例模式:JVM只存在一个实例。
特点:
1.拥有单例对象引用属性
2.私有的构造方法
3.提供公用的对外方法来获取单例对象
饿汉式简单实用,类加载就实例化了,并且是并发安全的。
/** * Every Java application has a single instance of class * <code>Runtime</code> that allows the application to interface with * the environment in which the application is running. The current * runtime can be obtained from the <code>getRuntime</code> method. * <p> * An application cannot create its own instance of this class. * * @author unascribed * @see java.lang.Runtime#getRuntime() * @since JDK1.0 */ 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() {} }
好记性不如烂笔头,提升自己:http://www.urlort.cn/1DjfQb
github地址:https://github.com/997480972