Java演示设计模式中的单件模式,单件模式是我们经常用到的,防止用户同时创建多个类实例
public class SimpleSingleton { private static SimpleSingleton singleInstance = new SimpleSingleton(); //Marking default constructor private //to avoid direct instantiation. private SimpleSingleton() { } //Get instance for class SimpleSingleton public static SimpleSingleton getInstance() { return singleInstance; } }
调用方法
public enum SimpleSingleton { INSTANCE; public void doSomething() { } } //Call the method from Singleton: // http://www.75271.com SimpleSingleton.INSTANCE.doSomething();