-
单例模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
-
1、单例类只能有一个实例。
-
2、单例类必须自己创建自己的唯一实例。
-
3、单例类必须给所有其他对象提供这一实例。
-
不建议使用第 1 种和第 2 种懒汉方式,建议使用第 3 种饿汉方式。只有在要明确实现 lazy loading 效果时,才会使用第 5 种登记方式。
-
如果涉及到反序列化创建对象时,可以尝试使用第 6 种枚举方式。如果有其他特殊的需求,可以考虑使用第 4 种双检锁方式。
package com.chenxb.service;
//第1种,【懒汉式】线程不安全、
//优点:需要时再初始化对象,不浪费内存。
public class Singleton {
//创建 Singleton 的一个对象
private static Singleton instance;
//让构造函数为 private,这样该类就不会被实例化
private Singleton(){}
//获取唯一可用的对象
public static Singleton getInstance(){
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
//第2种,【懒汉式】之(线程安全,通过synchronized加锁,但加锁会影响效率)
class Singleton2 {
private static Singleton2 instance;
private Singleton2 (){}
public static synchronized Singleton2 getInstance() {
if (instance == null) {
instance = new Singleton2();
}
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
//第3种,【饿汉式】,线程安全
//优点:没有加锁,执行效率会提高。
//缺点:类加载时就初始化,浪费内存。
class Singleton3 {
private static Singleton3 instance = new Singleton3();
private Singleton3 (){}
public static Singleton3 getInstance() {
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
//第4种,【双检锁/双重校验锁】(DCL,即 double-checked locking)
//优点:安全且在多线程情况下能保持高性能
class Singleton4 {
private volatile static Singleton4 singleton4;
private Singleton4 (){}
public static Singleton4 getInstance() {
if (singleton4 == null) {
synchronized (Singleton.class) {
if (singleton4 == null) {
singleton4 = new Singleton4();
}
}
}
return singleton4;
}
}
//第5种,【登记式/静态内部类】
//优点:能达到双检锁方式一样的功效,但实现更简单
//对静态域使用延迟初始化,应使用这种方式而不是双检锁方式。这种方式只适用于静态域的情况,双检锁方式可在实例域需要延迟初始化时使用。
//这种方式同样利用了 classloader 机制来保证初始化 instance 时只有一个线程,它跟第 3 种方式不同的是:第 3 种方式只要 Singleton 类被装载了,那么 instance 就会被实例化(没有达到 lazy loading 效果),
//而这种方式是 Singleton 类被装载了,instance 不一定被初始化。
//因为 SingletonHolder 类没有被主动使用,只有通过显式调用 getInstance 方法时,才会显式装载 SingletonHolder 类,从而实例化 instance。
//如果实例化 instance 很消耗资源,所以想让它延迟加载,另外一方面,又不希望在 Singleton 类加载时就实例化,因为不能确保 Singleton 类还可能在其他的地方被主动使用从而被加载,
//那么这个时候实例化 instance 显然是不合适的。这个时候,这种方式相比第 3 种方式就显得很合理。
class Singleton5 {
private static class SingletonHolder {
private static final Singleton5 INSTANCE = new Singleton5();
}
private Singleton5 (){}
public static final Singleton5 getInstance() {
return SingletonHolder.INSTANCE;
}
}
//第6种,【枚举】
class Singleton6 {
private static class SingletonHolder {
private static final Singleton6 INSTANCE = new Singleton6();
}
private Singleton6 (){}
public static final Singleton6 getInstance() {
return SingletonHolder.INSTANCE;
}
}
class SingletonPatternDemo {
public static void main(String[] args) {
//不合法的构造函数
//编译时错误:构造函数 Singleton() 是不可见的
//SingleObject object = new SingleObject();
//显示消息
System.out.println(Singleton.getInstance());
System.out.println(Singleton2.getInstance());
System.out.println(Singleton3.getInstance());
System.out.println(Singleton4.getInstance());
System.out.println(Singleton5.getInstance());
System.out.println(Singleton6.getInstance());
}
}