晴天的博客

编程基础之设计模式

本文主要介绍核心的设计模式以及一些具体的用法

todo

[[TOC]]

单例模式

1、懒汉模式、线程不安全

1
2
3
4
5
6
7
8
9
10
11
public class Singleton {
public static Singleton singleton;
private Singleton () {
}
public static Singleton getInstance() {
if (null == singleton) {
singleton = new Singleton();
}
return singleton;
}
}

2、懒汉模式,线程安全

1
2
3
4
5
6
7
8
9
10
11
public class Singleton {
public static Singleton singleton;
private Singleton () {
}
public static synchronized Singleton getInstance() {
if (null == singleton) {
singleton = new Singleton();
}
return singleton;
}
}

3、饿汉模式,线程安全

1
2
3
4
5
6
7
public class Singleton {
public static final Singleton singleton = new Singleton();
private Singleton() {}
public static Singleton getInstantce() {
return singleton;
}
}

4、DCL双重检查锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}

5、登记式\静态内部类,线程安全,延迟加载

1
2
3
4
5
6
7
8
9
public class Singleton {
public static class SingletonHodler {
public static final Singleton INSTANCE = new Singleton();
}
private Singleton() {}
public static final Singleton getInstance() {
return SingletonHodler.INSTANCE;
}
}

6、枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
public enum Singleton {
B {
@Override
public void whatever() {
super.whatever();
}
},

INSTANCE;
public void whatever() {

}
}