饿汉模式:
public class HungrySingle { private static final HungrySingle instance = new HungrySingle(); private HungrySingle(){ } public static HungrySingle getInstance(){ return instance; } }
懒汉模式:
public class LazySingle { private static LazySingle instance = null; private LazySingle(){ } public static LazySingle getInstance(){ if(instance==null){ synchronized (LazySingle.class){ if (instance==null){ instance = new LazySingle(); } } } return instance; } }