Java的23种设计模式之单例模式

很久之前,了解过Java的设计模式,一直没有深入研究过。现在回想,似乎也只有单例设计模式是我的真爱,久久不能忘怀。废话不多说,直接开干。

所谓的单例设计模式,顾名思义,就是保证系统运行过程中,某个类的实例是唯一的。那么我猜你肯定会想,为什么要保证类的实例的唯一呢?有两点:1.对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级的对象而言,是非常可观的一笔系统开销;2.由于new操作的次数减少,因而对系统内存的使用频率也会降低,这将减轻GC压力,缩短GC停顿时间。总而言之,就是为了提高系统的性能。

接下来,直接看代码实现吧~

一、单例之懒汉式(线程不安全的)

/*
 * 线程不安全的懒汉式单例设计模式
 * 
 * */
public class Singleton_unsupport_multithread {

	private Singleton_unsupport_multithread() {

	}

	private static Singleton_unsupport_multithread instance = null;

	public static Singleton_unsupport_multithread getInstance() {

		if (instance == null) {
			instance = new Singleton_unsupport_multithread();
		}
		return instance;
	}

}

 二、单例之懒汉式(线程安全的)

/*
 * 单例设计模式
 * 线程安全的懒汉式
 * 
 * */
public class Singleton_support_multithread {
	private Singleton_support_multithread() {

	}

	private static Singleton_support_multithread instance = null;

	public static synchronized Singleton_support_multithread getInstance() {

		if (instance == null) {
			instance = new Singleton_support_multithread();
		}
		return instance;
	}
}

 比较上面的两个懒汉式的单例模式发现,第二个之所以线程安全,是对具有原子性的资源添加了同步锁。但是synchronized限制了整个getInstance方法,而我们只是希望在new Singleton()时进行加锁,因此这种写法会导致效率不高。故而可以有以下的优化方式:

/*
 * 单例设计模式
 * 线程安全的懒汉式(优化后的)
 * 
 * */
public class Singleton_support_multithread {
	private Singleton_support_multithread() {

	}

	private static Singleton_support_multithread instance = null;

	public static Singleton_support_multithread getInstance() {
		if (instance == null) {
			
			synchronized (Singleton_support_multithread.class) {
				if (instance == null) {
					instance = new Singleton_support_multithread();
				}
			}
		}
		return instance;
	}
}

三、单例之饿汉式

饿汉式的单例设计模式很简单,类加载的时候就将对象初始化。故而,这也是它的缺陷,无论是否需要使用该类的实例,都会为其创建。

/*
 * 单例设计模式
 * 饿汉式
 * 
 * */
public class Singleton_hungry_man {
	
	private Singleton_hungry_man() {
		
	}
	
	private static Singleton_hungry_man instance = new Singleton_hungry_man();
	
	public static Singleton_hungry_man getInstance() {
		
		return instance;
	}	
}

四、单例之静态内部类

/*
 * 单例设计模式
 * 静态内部类
 * 
 * */
public class Singleton_innerclass {

	private Singleton_innerclass() {

	}

	private static class Inner {
		private static final Singleton_innerclass instance = new Singleton_innerclass();
	}

	public static Singleton_innerclass getInstance() {
		return Inner.instance;
	}
}

这种实现方式,用内部类来保护单例,当Singleton_innerclass类被加载时,内部类不会被初始化,所以可以确保Singleton_innerclass类被载入JVM时,不会初始化单例类,当getInstance方法被调用时,才会加载Inner,从而初始化instance,同时,由于实例的建立是在类加载时完成的,故天生对多线程友好,看起来有点完美,但是鄙人对内部类系列都有阴影,告辞。

五、单例之枚举

/*
 * 单例设计模式
 * 枚举
 * 
 * */
public enum Singleton_enum {

	instance;

	public void anyMethod() {
		
	}

}

足见枚举是个极其有意思的东西,它能保证序列化单例的能力是天生的。(后面会更一篇有关枚举的,再行详解)

六、单例之静态代码块

/*
 * 单例设计模式
 * 静态代码块
 * 
 * */
public class Singleton_static_code {

	private Singleton_static_code() {
		
	}
	
	private static Singleton_static_code instance = null;
	
	static {
		instance = new Singleton_static_code();
	}
	
	public static Singleton_static_code getInstance() {
		return instance;
	}
}

基本与饿汉式没什么区别,就不再多BB了。

写在最后:

单例模式的特点:1.单例类只能有一个实例;2.单例类必须自己创建自己的唯一实例;3.单例类必须给其他对象提供这一实例。

所以单例类的特点:1.私有的构造器;2.提供一个全局的获取该类实例的静态函数。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值