单例及面向对象语言中对全局变量控制的一种方式,比较简单,如下例所示:
public class DataSingleton { private static DataSingleton instance; public DataSingleton(){ System.out.println("DataSingleton created"); } //注意保证线程安全 public static synchronized DataSingleton getInstance(){ if(null == instance){ instance = new DataSingleton(); } return instance; } }
原文:http://www.cnblogs.com/Fredric-2013/p/4571066.html