单例及面向对象语言中对全局变量控制的一种方式,比较简单,如下例所示:
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; } }
时间: 2024-11-02 03:20:07