单例模式
1、单例设计模式的简介
采取一定的方法保证在整个软件系统中,某个类只存在一个对象实例
比如Hibernate的SessionFactory,他作为数据存储源的代理,负责创建Session对象,但是SessionFactory并不是轻量级的
,一般情况下,一个项目只需要一个SessionFactory就足够了,这就会使用到单例模式
2 单例设计模式的八种实现方式
3 饿汉式(静态常量)
/** * @author houChen * @date 2020/9/19 15:44 * @Description: 单例模式的饿汉式写法 */ public class SingletonTest01 { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); } } class Singleton{ //1、私有化构造器,这样外部就不能通过构造器来创建对象 private Singleton(){ } //2、本类内部创建对象实例 private final static Singleton instance = new Singleton(); //3、对外提供一个共有的静态方法,返回对象实例 public static Singleton getInstance(){ return instance; } }
4 饿汉式(静态代码块)
/** * @author houChen * @date 2020/9/19 15:44 * @Description: 单例模式的饿汉式写法(静态代码块) */ public class SingletonTest02{ public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); } } class Singleton{ //1、私有化构造器,这样外部就不能通过构造器来创建对象 private Singleton(){ } //2、本类内部创建对象实例 private static Singleton instance; //3、在静态代码块中创建单例对象 static { instance = new Singleton(); } //4、对外提供一个共有的静态方法,返回对象实例 public static Singleton getInstance(){ return instance; } }
5 懒汉式(线程不安全)
public class SingletonTest03 { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } } class Singleton{ private static Singleton instance; private Singleton(){}; //提供一个静态的公有方法,当使用到该方法时,才会去创建instance public static Singleton getInstance(){
// 多个线程进来时,会创建多个 Singleton实例,这就违反了单例模式的设计原则 if(instance==null){ instance = new Singleton(); } return instance; } }
优缺点:
6 懒汉式(使用同步方法解决线程安全问题)
/** * @author houChen * @date 2020/9/19 16:14 * @Description: 懒汉式 同步方法解决线程安全问题 */ public class SingletonTest04 { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } } class Singleton{ private static Singleton instance; private Singleton(){}; //提供一个静态的公有方法,当使用到该方法时,才会去创建instance public static synchronized Singleton getInstance(){ if(instance==null){ instance = new Singleton(); } return instance; } }
7 懒汉式(使用同步代码块)
public class SingletonTest04 { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } } class Singleton{ private static Singleton instance; private Singleton(){}; //提供一个静态的公有方法,当使用到该方法时,才会去创建instance public static Singleton getInstance(){ if(instance==null){ synchronized(Singleton.class){ instance = new Singleton(); } } return instance; } }
8 双重检查
/** * @author houChen * @date 2020/9/19 16:25 * @Description: 双重检查实现单例模式 */ public class SingletonTest06 { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } } class Singleton{ // volatile: 轻量级的synchronized, 由它修饰的变量在发生变化后会立即刷新到主存 private static volatile Singleton instance; private Singleton(){}; public static Singleton getInstance(){ if(instance==null){ synchronized (Singleton.class){ if(instance==null){ instance = new Singleton(); } } } return instance; } }
9 静态内部类
/** * @author houChen * @date 2020/9/19 16:59 * @Description: 静态内部类实现单例模式 */ public class SingletonTest07 { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); Singleton instance1 = Singleton.getInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } } class Singleton{ private Singleton(){ } //写一个静态内部类,该类中有一个静态属性instance private static class SingletonInstance{ private static final Singleton instance= new Singleton(); } //提供一个静态的共有方法 public static Singleton getInstance(){ return SingletonInstance.instance; } }
10 枚举实现单例模式
/** * @author houChen * @date 2020/9/19 17:06 * @Description: 枚举实现单例模式 */ public class SingletonTest08 { public static void main(String[] args) { Singleton instance = Singleton.INSTANCE; Singleton instance1 = Singleton.INSTANCE; System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } } //使用枚举 可以实现单例 enum Singleton{ INSTANCE; public void sayOk(){ System.out.println("say ok~"); } }