JAVA设计模式-单例模式
创建性模式
? 创建性模式主要有五个,主要用于创建对象。
- 单例模式
- 工厂方法模式
- 抽象工厂模式
- 原型模式
- 构建器模式
单例模式
? 采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例方法。有八种写法:
写法一:饿汉式(静态常量)
优缺点说明:
- 优点:这种写法比较简单,就是在装载的时候就完成实例化,避免了线程同步的问题。
- 缺点:在类装载的时候就完成实例化,没有达到Lazy Loading的效果。如果从开始至终从未使用过这个实例,则会造成内存的浪费。instance会在类装载的时候就被加载,但是导致类装载的原因有很多种,因此不能确定有其他的方式(或者其他的静态方法)导致类装载,这种时候初始化instance就没有达到lazy loading的效果
结论:这种单例模式可用,可能会造成内存浪费
实现步骤如下:
- 构造器私有化
- 类的内部创建对象
- 向外暴露一个静态的公共方法
getInstance()
- 代码实现
public static void main(String[] args) {
//测试
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1== instance2);// true 验证方法1
System.out.println(instance1.hashCode() == instance2.hashCode());// true 验证方法2
}
}
class Singleton{
//step1: 构造器私有化
private Singleton() {
}
// step2:本类内部创建对象实例
private final static Singleton instance = new Singleton();
//step3: 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
写法二:饿汉式(静态代码块)
? 和上面【静态常量】的方法基本一致,可能会造成内存浪费
public static void main(String[] args) {
//测试
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1== instance2);// true 验证方法1
System.out.println(instance1.hashCode() == instance2.hashCode());// true 验证方法2
}
}
class Singleton{
//step1: 构造器私有化
private Singleton() {
}
// step2:本类内部创建对象实例
private static Singleton instance ;
static {//在静态代码块中,创建单例对象
instance = new Singleton();
}
//step3: 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
}
写法三:懒汉式(线程不安全)
优缺点说明:
- 起到了Lazy Loading的效果,但是只能在单线程下使用
- 如果在多线程的情况下,如果有一个线程进入到
if(instance ==null)
判断语句,这便会产生多个实例,所以多线程情况下不可使用。结论:
? 在实际开发中,不要使用这种写法。
public static void main(String[] args) {
// 懒汉式, 线程不安全
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1== instance2);// true 验证方法1
System.out.println(instance1.hashCode() == instance2.hashCode());// true 验证方法2
}
}
class Singleton{
private static Singleton instance;
private Singleton() {}// 私有化构造方法,防止对象被创建
// 提供一个静态的公共方法,当使用到该方法时,才去创建instance 懒汉式
public static Singleton getInstance() {
if(instance ==null) {
instance = new Singleton();
}
return instance;
}
}
写法四:懒汉式(线程安全)
优缺点说明:
- 解决了线程不安全的问题
- 效率太低了,每个线程在想获得类的实例时候,执行
getInstance()
方法都要进行同步,方法执行效率太低结论:
? 在实际开发过程中,不推荐使用这种写法
public static void main(String[] args) {
// 懒汉式, 线程不安全
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1== instance2);// true 验证方法1
System.out.println(instance1.hashCode() == instance2.hashCode());// true 验证方法2
}
}
class Singleton{
private static Singleton instance;
private Singleton() {}// 私有化构造方法,防止对象被创建
// 提供一个静态的公共方法,当使用到该方法时,才去创建instance 懒汉式
public static synchronized Singleton getInstance() {
if(instance ==null) {
instance = new Singleton();
}
return instance;
}
}
扩展,可以将
instance = new Singleton()
转换为synchronized(Singleton.class){ instance = new Singleton()}
但是这种写法是无效,和上面写法的效果是一样的。
写法五:双重检查【推荐】
? 【扫盲】volatile 保证 多线程之间的可见性:理解为: 多个线程对一个变量同时操作;在
java
的内存模型中,多个线程同时拥有这个变量的副本,同时操作就造成了这个变量线程不安全;使用volatile关键字可以使被修饰的对象在多线程中具有可见性,也就是多个线程中使用到的该对象永远都是最新的值。比synchronize的性能好。优缺点说明:
Double-Check
概念是多线程开发中常使用到的,如代码中所示,我们进行了两次if(singleton == null)
检查,这样就可以保证线程安全。- 这样,实例化代码就只执行一次,后面再访问时,判断
if(singleton == null)
,直接return实例化对象,也避免的反复进行方法同步。- 线程安全;延迟加载;效率较高
结论:在实际开发中,推荐使用这种单例模式。
public static void main(String[] args) {
// 懒汉式, 线程不安全
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1== instance2);// true 验证方法1
System.out.println(instance1.hashCode() == instance2.hashCode());// true 验证方法2
}
}
class Singleton{
private static volatile Singleton instance;
private Singleton() {}// 私有化构造方法,防止对象被创建
// 提供一个静态的公共方法,当使用到该方法时,才去创建instance 懒汉式
public static synchronized Singleton getInstance() {
if(instance == null) {
synchronized(Singleton.class) {
if(instance ==null) {
instance = new Singleton();
}
}
}
return instance;
}
}
写法六:静态内部类【推荐】
优缺点说明
- 这种方法采用了类装载的机制来保证初始化实例时只有一个线程
- 静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化
- 类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。
- 优点 避免了线程不安全,利用静态内部类特点实现延迟加载,效率高
结论:推荐使用。
public static void main(String[] args) {
// 懒汉式, 线程不安全
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1== instance2);// true 验证方法1
System.out.println(instance1.hashCode() == instance2.hashCode());// true 验证方法2
}
}
class Singleton{
private static volatile Singleton instance;
private Singleton() {}// 私有化构造方法,防止对象被创建
// 提供一个静态内部类,该类中有一个静态属性Singleton
private static class SingletonInstance{
private static final Singleton INSTANCE = new Singleton();
}
// 提供一个静态的公共方法,当使用到该方法时,才去创建instance 懒汉式
public static Singleton getInstance() {
return SingletonInstance.INSTANCE;
}
}
写法七:枚举方法【推荐】
优缺点说明:
- 借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新对象。
- 这种方式是Effective Java做着 JoshBloch提倡的方式
结论:推荐使用。
public static void main(String[] args) {
Singleton instance = Singleton.INSTANCE;
Singleton instance2 = Singleton.INSTANCE;
System.out.println(instance == instance2);
System.out.println(instance.hashCode() == instance.hashCode());
instance.sayOK();
}
}
//使用枚举,可以实现单例,推荐使用
enum Singleton{
INSTANCE;
public void sayOK(){
System.out.println("OK~");
}
}