单例设计模式是Java中应用最为普遍的设计模式之一,保证了一个类始终只有一个对象,具备如下特色:java
这里1有一个关于singleton的故事,一个国家只能有且仅有一个president,president只能被实例化一次,getPresident()返回这个仅有的president。设计模式
public class AmericaPresident { private static final AmericaPresident thePresident = new AmericaPresident(); private AmericaPresident() {} public static AmericaPresident getPresident() { return thePresident; } }
class Runtime { private static Runtime currentRuntime = new Runtime(); public static Runtime getRuntime() { return currentRuntime; } private Runtime() {} //... }