单例模式
简介
类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得器对象实例的方法(静态方法)。
实现方式
共有7种实现方式:
饿汉式(静态常量)可使用
饿汉式:就是需要提前备好资源。
在实际实现时,需要做以下几点:
- 构造器私有化(防止new)
- 类的内部创建对象
- 对外暴露一个静态的公共方法,用于获取内部创建单例对象。getInstance
样例:
//饿汉式(静态变量)
class Singleton {
//1. 构造器私有化, 外部能new
private Singleton() {
}
//2.本类内部创建对象实例
private final static Singleton instance = new Singleton();
//3. 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
}
优点:
- 写法简单
- 实例化过程直接在类加载时完成,
- 线程安全
缺点:
没有lazy loading效果,可能造成内存浪费
实际开发中可使用
饿汉式(静态代码块)可使用
和饿汉式(静态常量)类似,只是类实例化时通过静态代码块完成。
代码样例:
class Singleton {
//1. 构造器私有化, 外部能new
private Singleton() {
}
//2.本类内部创建对象实例
private static Singleton instance;
static { // 在静态代码块中,创建单例对象
instance = new Singleton();
}
//3. 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
}
优缺点和饿汉式(静态常量)一样
懒汉式(线程不安全)不可用
懒汉式:即实现lazy loading效果。
代码样例:
class Singleton {
private static Singleton instance;
private Singleton() {}
//提供一个静态的公有方法,当使用到该方法时,才去创建 instance
//即懒汉式
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
优点:达到了lazy loading 效果,但只能单线程使用
缺点:线程不安全,多线程时,会重复创建
懒汉式(线程安全,同步方法)不推荐使用
// 懒汉式(线程安全,同步方法)
class Singleton {
private static Singleton instance;
private Singleton() {}
//提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
//即懒汉式
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
优缺点:
解决了懒汉式(线程不安全)的线程不安全问题,但导致效率大幅度降低。
不推荐使用
双重检查,推荐使用
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;
}
}
点评:
通过使用synchronized前后两次if语句,实现线程安全。
并且达到了lazy loading效果,效率高不会因为synchronized降低效率。
推荐使用
静态内部类,推荐使用
// 静态内部类完成, 推荐使用
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;
}
}
优缺点说明:
通过类装载的机制来保证初始化实例只有一个线程
静态内部类方式在Singleton被装载时不会立刻实例化,而是在需要实例化,调用getInstance方法时,才会被实例化
类的静态熟悉只会在第一次加载类的时候初始化,JVM保证了线程的安全性,在类进行初始化时,别的线程是无法进入的
优点:避免了线程不安全,利用静态内部类特点实现延迟加载,效率高
推荐使用
枚举(推荐使用,提倡使用)
//使用枚举,可以实现单例, 推荐
enum Singleton {
INSTANCE; //属性
public void sayOK() {
System.out.println("ok~");
}
}
优缺点说明:
借助枚举实现单例,既能避免多线程同步问题,而且还能防止反序列化重新创建新的对象
在JDK上的应用
java.lang.Runtime就是单例模型(饿汉式)
public class Runtime {
private static Runtime currentRuntime = new Runtime();
/**
* Returns the runtime object associated with the current Java application.
* Most of the methods of class <code>Runtime</code> are instance
* methods and must be invoked with respect to the current runtime object.
*
* @return the <code>Runtime</code> object associated with the current
* Java application.
*/
public static Runtime getRuntime() {
return currentRuntime;
}
/** Don't let anyone else instantiate this class */
private Runtime() {}
}
注意事项
-
单例模式保证了系统内存中该类只存在一个对象,节省了系统资源。
-
使用场景:需要频繁的进行创建和销毁的对象、创建对象时耗时过多或耗费资源过多,但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(如数据源、session工厂)