设计原则
设计模式
各种模式都有自己的特定使用情景
单例模式
对本类构造方法私有化 ,防止外部调用构造方法创建对象
创建全局唯一的静态对象,也做私有化处理
通过自定义的公共方法将创建好的对象返回(类似封装属 性后的getXxx()
)
饿汉式
先把该对象创建出来,等待被使用
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() {}
}