单例模式

设计模式分为三种类型:创建型模式、结构型模式、行为型模式,共23种。

单例设计模式

单例设计模式属于创建型模式,就是采取一定的方法保证整个软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个获取该对象实例的方法(静态方法)。

单例模式的几种写法

饿汉式(静态常量)

步骤:
1、构造器私有化;
2、类内部创建一个静态的对象实例并私有化;
3、对外暴露一个静态的公共方法getInstance()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class SingletonTest1 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1==instance2);// true
}
}

class Singleton{
// 私有化构造器
private Singleton(){

}

// 创建静态实例对象
private static final Singleton singleton = new Singleton();

// 对外暴露获取实例的静态方法
public static Singleton getInstance(){
return singleton;
}
}

优点:在类装载时就完成了对象的实例化,避免了线程同步问题。
缺点:由于在类加载时就完成了类的实例化,没有达到懒加载的效果,如果这个实例对象一直没有使用过,就会造成内存的浪费。

饿汉式(静态代码块)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.zyz.singleton;

public class SingletonTest2 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1==instance2);// true
}
}

class Singleton{
// 私有化构造器
private Singleton(){

}

// 创建静态对象
private static Singleton singleton;

// 在静态代码块中实例化对象
static {
singleton = new Singleton();
}

// 对外暴露获取实例的静态方法
public static Singleton getInstance(){
return singleton;
}
}

懒汉式(线程不安全)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class SingletonTest3 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1==instance2);// true
}
}

class Singleton{
// 私有化构造器
private Singleton(){

}

// 私有化静态变量
private static Singleton singleton;

// 暴露获取实例对象的静态方法,使用该方法才创建对象
public static Singleton getInstance(){
// 对象实例不存在就创建,否则直接返回
if(singleton==null){
singleton = new Singleton();
}
return singleton;
}
}

达到了懒加载的效果,但是存在线程安全问题,在多线程环境下可能会创建多个对象实例。

懒汉式(线程安全,同步方法)

使用synchronized关键字修饰getInstance()方法

双重检查

解决了线程安全问题,懒加载问题,同时保证了效率,在实际开发中推荐使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class SingletonTest4 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);// true
}
}

class Singleton {

// 私有化构造器
private Singleton() {

}

// 私有化静态变量
// 使用volatile保证变量在多个线程之间的可见性,防止指令重排列
private static volatile Singleton singleton;

// 暴露获取实例对象的静态方法,使用该方法才创建对象
public static Singleton getInstance() {
// 双重检查,对象实例不存在就创建,否则直接返回
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}

静态内部类

静态内部类不会自动加载,只有调用静态内部类的方法,静态域,或者构造方法的时候才会加载,并且只加载一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class SingletonTest5 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);// true
}
}

class Singleton {

// 私有化构造器
private Singleton() {

}

// 使用静态内部类,提供一个实例
private static class SingletonInstance{
private static final Singleton INSTANCE = new Singleton();
}

// 暴露获取实例对象的静态方法,使用该方法才加载静态内部类,并返回一个实例对象
public static Singleton getInstance() {
return SingletonInstance.INSTANCE;
}
}

枚举

1
2
3
4
5
6
7
8
9
10
11
public class SingletonTest6 {
public static void main(String[] args) {
Singleton instance1 = Singleton.INSTANCE;
Singleton instance2 = Singleton.INSTANCE;
System.out.println(instance1 == instance2);// true
}
}

enum Singleton {
INSTANCE;
}

单例模式的使用场景

  • 需要频繁创建和销毁的对象
  • 创建对象时耗时过多或耗费资源过多
  • 工具类对象
  • 频繁访问数据库或文件的对象(比如数据源、session工厂等)