设计模式分类
单例模式
、
所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例 ,并且该类只提供一个取得其对象实例的方 法 静态方法 。
比如
Hibernate 的 SessionFactory ,它充当数据存储源的代理,并负责创建 Session对象。 SessionFactory 并不是轻量级的,一般情况下,一个项目通常只需要一个SessionFactory 就够,这是就会使用到单例模式。
单例设计模式八种方式:
1)饿汉式 - 静态常量
public class SingletonTest01 { public static void main(String[] args) { //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance + " === " + instance2); System.out.println(instance == instance2); System.out.println("instance.hashCode()==" + instance.hashCode()); System.out.println("instance2.hashCode()==" + instance2.hashCode()); } } /** * 饿汉式(静态常量) */ class Singleton{ //1.构造器私有化,外部不能new private Singleton(){} //2.本类内部创建对象实例 private final static Singleton instance = new Singleton(); //3.提供一个公有的静态方法,返回实例对象 public static Singleton getInstance(){ return instance; } }
优缺点:
2)饿汉式 - 静态代码块
public class SingletonTest02 { public static void main(String[] args) { //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance + " === " + instance2); System.out.println(instance == instance2); System.out.println("instance.hashCode()==" + instance.hashCode()); System.out.println("instance2.hashCode()==" + instance2.hashCode()); } } /** * * 饿汉式(静态代码块) */ class Singleton{ //1.构造器私有化,外部不能new private Singleton(){} //2.本类内部创建对象实例 private static Singleton instance; static { //在静态代码块中,创建单例对象 instance = new Singleton(); } //3.提供一个公有的静态方法,返回实例对象 public static Singleton getInstance(){ return instance; } }
优缺点:
3)懒汉式- 线程不安全
public class SingletonTest03 { public static void main(String[] args) { System.out.println("懒汉式1 , 线程不安全~"); Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } class Singleton { private static Singleton instance; private Singleton() {} //提供一个静态的公有方法,当使用到该方法时,才去创建 instance //即懒汉式 public static Singleton getInstance() { if(instance == null) { instance = new Singleton(); } return instance; } }
优缺点
4)懒汉式 - 线程安全,同步方法
public class SingletonTest04 { public static void main(String[] args) { System.out.println("懒汉式2,线程安全"); //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance + " === " + instance2); System.out.println(instance == instance2); System.out.println("instance.hashCode()==" + instance.hashCode()); System.out.println("instance2.hashCode()==" + instance2.hashCode()); } } /** * 懒汉式(线程安全,同步方法) */ class Singleton{ private static Singleton instance; private Singleton(){ } public static synchronized Singleton getInstance(){ if (instance == null){ instance = new Singleton(); } return instance; } }
优缺点
5)懒汉式 - 线程安全,同步代码块
public class SingletonTest05 { public static void main(String[] args) { System.out.println("懒汉式2,线程安全,同步代码块"); //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance + " === " + instance2); System.out.println(instance == instance2); System.out.println("instance.hashCode()==" + instance.hashCode()); System.out.println("instance2.hashCode()==" + instance2.hashCode()); } } /** * 懒汉式(线程安全,同步代码块) */ class Singleton{ private static Singleton instance; private Singleton(){ } public static Singleton getInstance(){ if (instance == null){ synchronized(Singleton.class){ instance = new Singleton(); } } return instance; } }
优缺点:
6)双重检查
public class SingletonTest06 { public static void main(String[] args) { System.out.println("懒汉式,双重检查"); //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance + " === " + instance2); System.out.println(instance == instance2); System.out.println("instance.hashCode()==" + instance.hashCode()); System.out.println("instance2.hashCode()==" + instance2.hashCode()); } } //懒汉式(双重检查) class Singleton{ 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; } }
优缺点:
7)静态内部类
public class SingletonTest07 { public static void main(String[] args) { System.out.println("使用静态内部类完成单例模式"); Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } // 静态内部类完成, 推荐使用 class Singleton { private static volatile Singleton instance; //构造器私有化 private Singleton() {} //写一个静态内部类,该类中有一个静态属性 Singleton private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } //提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE public static synchronized Singleton getInstance() { return SingletonInstance.INSTANCE; } }
优缺点:
8)枚举
public class SingletonTest08 { public static void main(String[] args) { Singleton instance = Singleton.INSTANCE; Singleton instance2 = Singleton.INSTANCE; System.out.println(instance == instance2); System.out.println(instance.hashCode()); System.out.println(instance2.hashCode()); instance.sayOK(); } } //使用枚举,可以实现单例, 推荐 enum Singleton { INSTANCE; //属性 public void sayOK() { System.out.println("ok~"); } }
优缺点:
实际单例的使用:
单例模式在 JDK 应用的源码分析
1)我们 JDK 中 java.lang.Runtime 就是经典的单 例 模 式 饿汉式
2)代码分析 +Debug 源码 代码说明
单例模式注意事项和细节说明
完