编写Java的(单例模式)单实例Singleton 的示例

代码分享如下:

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: SimpleSingleton.INSTANCE.doSomething();

--Hurry

你可能感兴趣的:(编写Java的(单例模式)单实例Singleton 的示例)