首页 > 编程学习 > 设计模式-第18章(单例模式)

单例模式

  • 单例模式
  • 多线程下的单例模式
  • 双重检测锁
  • 静态初始化

单例模式

单例模式(Singleton),保证一个类仅有一个实例,并提供一个访问它的全局访问点。

通常可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个对象。一个最好的办法就是,让类自身负责保存它的唯一实例。这个类可以保证没有其他实例可以被创建,并且它可以提供一个访问该实例的方法。

在这里插入图片描述
Singleton类,包含了一个实例化的对象,定义一个 getInstance 静态方法,主要用来得到唯一的实例。将构造方法私有化,禁止其他地方进行对象的实例化。

单例模式类

class Singleton {private static Singleton instance;//构造方法private化private Singleton() {}//得到Singleton的实例(唯一途径)public static Singleton getInstance() {if (instance == null) {instance = new Singleton();}return instance;}
}

客户端类

public class Test {public static void main(String[] args) {System.out.println("**********************************************");       System.out.println("《大话设计模式》代码样例");System.out.println();       //Singleton s0 = new Singleton();Singleton s1 = Singleton.getInstance();Singleton s2 = Singleton.getInstance();if (s1 == s2) {System.out.println("两个对象是相同的实例。");}System.out.println();System.out.println("**********************************************");}
}

单例模式因为 Singleton类封装了唯一的实例,单例类实现了对唯一实例的受控访问。

多线程下的单例模式

在多线程环境下,如果同时访问 Singleton 类,调用getInstance方法,可能会创建多个实例。
可以在 getInstance 方法上加锁,保证了同一时间只能有一个线程进入 getInstance 方法内部。

//单例模式类
class Singleton {private static Singleton instance;//构造方法private化private Singleton() {}//得到Singleton的实例(唯一途径)public static synchronized Singleton getInstance() {if (instance == null) {instance = new Singleton();}return instance;}
}

这样,在每次调用 getInstance 方法时都需要加锁。会影响性能。

双重检测锁

可以在getInstance方法内部对 Singleton 类加锁,只有当对象没有实例化的时候,加锁,防止多个线程创建多个对象。减少性能消耗。

//单例模式类
class Singleton {private volatile static Singleton instance;//构造方法private化private Singleton() {}//得到Singleton的实例(唯一途径)public static Singleton getInstance() {if (instance == null){// 在没有实例化对象的时候才加锁。synchronized(Singleton.class){// 双重检测。if (instance == null){instance = new Singleton();}}}return instance;}
}

当instance对象为null,并且同时有两个线程调用 getInstance方法,它们都能通过第一重,instance == null 的判断。然后,由于锁机制,只能有一个线程进入,进行对象的创建,然后解锁。然后第二个线程加锁进入,如果没有第二重检测,则第二个线程便可创建新的实例。

静态初始化

在类被加载的时候,就进行实例化。这种方法被称为饿汉式单例。不管有没有用到,先实例化再说。

//单例模式类
class Singleton {// 静态属性,在类加载的时候,进行实例化。private static Singleton instance = new Singleton();//构造方法private化private Singleton() {}//得到Singleton的实例(唯一途径)public static Singleton getInstance() {return instance;}
}

原先的单例模式,是在第一次被引用的时候,才将自己实例化。这种方式被称为懒汉式单例类。


本文链接:https://www.ngui.cc/article/show-985523.html
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000