微信搜索:编程笔记本。获取更多干货。
点击上方蓝字关注我,我们一起学编程
欢迎小伙伴们分享、转载、私信、赞赏
单例模式
文章目录
所谓类的单例设计模式,就是采取一定的方法保证在整个软件系统中,对某个类 只存在一个对象实例,并且该类只提供一个取得该对象实例的方法(静态方法)。
步骤:
- 构造器私有化(防止 new)
- 类的内部创建对象
- 向外暴露一个静态的公共方法(getInstance)
1. 饿汉式(静态常量)
代码实现:
// 饿汉式(静态常量)
public class test {
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 final static Singleton instance = new Singleton();
// 提供一个公有的静态方法,返回对象实例
public static Singleton getInstance() {
return instance;
}
}
优点:
- 写法比较简单,在类装载时就完成实例化,避免了线程同步问题。
缺点:
- 由于在类装载时就完成实例化,若从未使用到这个实例,就会造成内存浪费。
结论:
- 简单,可用,可能造成内存浪费。
2. 饿汉式(静态代码块)
代码实现:
// 饿汉式(静态代码块)
public class test {
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 instance;
// 在静态代码块中创建单例对象
static {
instance = new Singleton();
}
// 提供一个公有的静态方法,返回对象实例
public static Singleton getInstance() {
return instance;
}
}
说明:
- 这种方式和饿汉式(静态常量)方式类似,只不过将类的实例化过程放在了静态代码块中,也是在类装载时就执行代码块中的代码,初始化类的实例。
- 优缺点与结论同上。
3. 懒汉式(线程不安全)
代码实现:
// 懒汉式(线程不安全)
public class test {
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 instance;
// 提供一个公有的静态方法,当使用该方法时才去创建实例
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
优点:
- 起到 “lazy loading” 的效果,即只有需要使用这个实例我们才会创建它。
缺点:
- 只能在单线程下使用。若在多线程情况下,多个线程同时进入
if
判断时,就可能会创建多个实例。
结论:
- 在实际开发中,不要使用这种方式。
4. 懒汉式(线程安全,同步方法)
代码实现:
// 懒汉式(线程安全,同步方法)
public class test {
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 instance;
// 加入同步处理的代码,解决线程安全问题
// 提供一个公有的静态方法,当使用该方法时才去创建实例
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
优点:
- 解决线程不安全的问题。
缺点:
- 每次获取实例时都需要进行同步操作,效率低。实际上,同步操作只在第一次获取实例时需要,以后都不需要。
结论:
- 在实际开发中,不建议使用这种方式。
5. 懒汉式(线程安全,同步代码块) x
代码实现:
// 懒汉式(线程安全,同步代码块)
public class test {
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 instance;
// 加入同步处理的代码,解决线程安全问题
// 提供一个公有的静态方法,当使用该方法时才去创建实例
public static Singleton getInstance() {
if (instance == null) {
// 同步操作
instance = new Singleton();
// 同步操作
}
return instance;
}
}
说明:
- 该方式其实是不能保证线程安全的。在这里展示出来只是说明有很多开发人员这样用,但其实是不正确的。
- 当多个线程同时执行完
if
判断时,均会进行对象实例化,由于同步的作用,只是时间先后罢了。 - 在实际开发中,不能使用这种方式。
6. 双重检查
代码实现:
// 双重检查
public class test {
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 instance;
// 加入双重检查
// 提供一个公有的静态方法,当使用该方法时才去创建实例
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
优点:
- 保证了线程安全,实现了 “lazy loading” 的效果,并避免了不必要的同步操作,保证了效率。
结论:
- “Double-Check” 概念在多线程开发中经常被使用到。
- 在实际开发中,推荐使用这种方式。
7. 静态内部类
代码实现:
// 静态内部类
public class test {
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;
}
}
优点:
- 利用静态内部类特点实现延迟加载。Singleton 类被装载时不会使得 SingletonInstance 类的装载,只有调用
getInstance()
方法时才会装载 SingletonInstance 类,从而完成类的实例化。 - 线程安全。类的静态属性只会在第一次加载类时被初始化,在 JVM 会保证在类进行初始化时,别的线程是无法进入的。
结论:
- 在实际开发中,推荐使用这种方式。
8. 枚举
代码实现:
// 枚举
public class test {
public static void main(String[] args) {
// 测试
Singleton instance1 = Singleton.instance;
Singleton instance2 = Singleton.instance;
System.out.println(instance1 == instance2); // true
}
}
// 使用枚举可以实现单例
enum Singleton {
instance;
}
优点:
- 线程安全,且还能防止反序列化重新创建新的对象。
结论:
- 在实际开发中,推荐使用这种方式。