设计模式之单例模式
单例模式的用途
单例模式保证了系统中某个类只有一个实例,而且自行实例化并向整个系统提供自己。在计算机系统中,线程池、日志对象、缓存、对话框、打印机等都被设计成单例。spring默认也是单例模式,单例模式一方面可以控制实例产生的数量,提高垃圾回收的效率,另一方面也可以作为通信的媒介,使不同线程实现通信。
单例模式的写法:
关键是用private关键字修饰构造函数,使程序中不能通过new的方法创建对象。然后用懒汉模式或者饿汉模式来实现单例模式。
package designMode.singleton;
public class SingleLazyUnsafe {
public void print(){
System.out.println(SingleLazyUnsafe.class.getName());
}
//构造函数定义为私有的,防止被调用
private SingleLazyUnsafe(){};
private static SingleLazyUnsafe instance;
//若instance为空,进行初始化,运行时初始化,if判断没有加synchronized,线程不安全
public static SingleLazyUnsafe getInstance(){
if(instance == null){
instance = new SingleLazyUnsafe();
}
return instance;
}
}
package designMode.singleton;
public class SingleLazySafeOne {
public void print(){
System.out.println(SingleLazySafeOne.class.getName());
}
//构造函数定义为私有的,防止被调用
private SingleLazySafeOne(){};
private static SingleLazySafeOne instance;
//若instance为空,进行初始化,运行时初始化,线程安全,但并发效率低
public static synchronized SingleLazySafeOne getInstance(){
if(instance == null){
instance = new SingleLazySafeOne();
}
return instance;
}
}
package designMode.singleton;
public class SingleLazySafeTwo {
public void print(){
System.out.println(SingleLazySafeTwo.class.getName());
}
//构造函数定义为私有的,防止被调用
private SingleLazySafeTwo(){};
private static volatile SingleLazySafeTwo instance;
//若instance为空,进行初始化,运行时初始化,代码块加synchronized线程安全,效率高
public static SingleLazySafeTwo getInstance(){
if(instance == null){
synchronized(SingleLazySafeTwo.class){
if(instance == null){
//此处由于new 不是原子性动作,因此instance需要用volatile进行修饰
instance = new SingleLazySafeTwo();
}
}
}
return instance;
}
}
package designMode.singleton;
public class SingleHungry {
public void print(){
System.out.println(SingleHungry.class.getName());
}
private SingleHungry(){};
private static SingleHungry instance = new SingleHungry();
public static SingleHungry getInstance(){
return instance;
}
}
package designMode.singleton;
public class SingleFinal {
public void print(){
System.out.println(SingleFinal.class.getName());
}
private SingleFinal(){}
// 静态内部类只有在被调用时才初始化,且只会被初始化一次
private static class InstanceHolder{
private static final SingleFinal instance = new SingleFinal();
}
public static SingleFinal getInstance(){
return InstanceHolder.instance;
}
}
package designMode.singleton;
//枚举方式实现单例模式
public enum SingleEnum {
INSTANCE;
public void print(){
System.out.println(SingleEnum.class.getName());
}
}
package designMode.singleton;
//测试用例
public class SingleObject {
public static void main(String[] args){
SingleLazyUnsafe singleLazyUnsafe = SingleLazyUnsafe.getInstance();
singleLazyUnsafe.print();
SingleLazySafeOne singleLazySafeOne = SingleLazySafeOne.getInstance();
singleLazySafeOne.print();
SingleLazySafeTwo singleLazySafeTwo = SingleLazySafeTwo.getInstance();
singleLazySafeTwo.print();
SingleHungry singleHungry = SingleHungry.getInstance();
singleHungry.print();
SingleFinal singleFinal = SingleFinal.getInstance();
singleFinal.print();
SingleEnum singleEnum = SingleEnum.INSTANCE;
singleEnum.print();
}
}
本文探讨设计模式中的单例模式,详细解释其用途,如控制实例数量、提升垃圾回收效率,以及作为多线程间通信的媒介。同时,介绍了实现单例模式的两种常见方法:懒汉模式和饿汉模式,强调了构造函数的私有化在单例模式中的关键作用。
269

被折叠的 条评论
为什么被折叠?



