作用
- 节省内存;
- 符合实际生活中的业务模型;
实现
单例模式的实现有两种:
懒汉式
12345678910public class Singleton {private Singleton(){}private static Singleton uniqueInstance = null;public synchronized static Singleton getInstance(){if(uniqueInstance == null){uniqueInstance = new Singleton();}return uniqueInstance;}}饿汉式
1234567public class Singleton {private Singleton(){}private static Singleton uniqueInstance = new Singleton();public static Singleton getInstance(){return uniqueInstance;}}