1 单例模式介绍
1.1单例模式定义
单例模式是设计模式的一种,它确保了一个类只能创建一个实例,并且提供能够访问他的全局访问点。
1.2单例模式的特点
- 保证自己只有一个实例
- 自己创建自己的实例
- 对外提供一个全局访问点
1.3单例模式的优点
- 减少创建java实例所带来的系统开销,提高了系统效率
- 能够严格控制客户对它的访问
- 便于系统跟踪单个java实例的生命周期、实例状态
1.4单例模式应用场景
当系统只需要一个实例对象时可以使用单例模式,如:数据库连接池、线程池、配置类、管理类、spring默认管理对象的方式等。
2 单例模式的实现方式
2.1饿汉式
/**
* 饿汉式:加载类的时候就创建实例,效率高,不能实现懒加载,适合初始化就会被使用的类
*/
public class SingletonHungry {
private static SingletonHungry instance = new SingletonHungry();
//私有化构造,保证外界无法直接访问
private SingletonHungry() {
}
//提供全局唯一的访问点获取实例
public static SingletonHungry getInstance(){
return instance;
}
}
2.2懒汉式
/**
* 懒汉式:可以实现延迟加载,但由于用到锁,效率没有恶汉式高
*/
public class SingletonLazy {
private static volatile SingletonLazy instance;//new对象不是原子操作,这里用volatile修饰;
private SingletonLazy() {
}
public SingletonLazy getInstance(){
if (null == instance) {
synchronized (this) {
if(null == instance) {
instance = new SingletonLazy();
}
}
}
return instance;
}
}
volatile更多介绍:https://blog.youkuaiyun.com/ACreazyCoder/article/details/80982578
2.3静态内部类实现
//使用静态内部类实现单例模式,可以懒加载
public class Singleton implements Serializable {
private Singleton() {
}
private static class SingletonInner{
private static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonInner.instance;
}
}
3单例模式破解与避免
3.1反射破解
以前面的饿汉式为例,代码如下:
public class Test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
SingletonHungry instance = SingletonHungry.getInstance();
//反射破解单例
Class cls = SingletonHungry.class;
Constructor con = cls.getDeclaredConstructor(null);
con.setAccessible(true);//关闭安全检查,获取私有构造
SingletonHungry instance1 = (SingletonHungry)con.newInstance(null);
System.out.println(instance.hashCode());
System.out.println(instance1.hashCode());
}
}
实例一与实例二的哈希值不同,单例被破解
3.2避免反射破解单例的方法
反射是通过它的Class对象来调用构造器创建出新的对象,我们只需要在构造器中手动抛出异常,导致程序停止就可以达到目的了,代码如下:
public class SingletonHungry {
private static SingletonHungry instance = new SingletonHungry();
//私有化构造,保证外界无法直接访问
private SingletonHungry() {
//如果instance不为null抛出异常,防止反射获取类的实例
if (null != instance){
throw new RuntimeException("通过反射非法获取类的实例");
}
}
//提供全局唯一的访问点获取实例
public static SingletonHungry getInstance(){
return instance;
}
}
可以看出,当通过反射去获取类的实例时会抛出异常!
3.3序列化方式破解单例
public static void main(String[] args) throws IOException, ClassNotFoundException {
//获取单例对象
Singleton instance = Singleton.getInstance();
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("D://singleton.txt"));
//序列化
oos.writeObject(instance);
oos.flush();
oos.close();
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("D://singleton.txt"));
//反序列化获取对象
Singleton instanceIo = (Singleton) ois.readObject();
System.out.println("序列化前hash:"+instance.hashCode());
System.out.println("序列化后hash:"+instanceIo.hashCode());
}
3.4避免序列化破解方法
public class Singleton implements Serializable {
private Singleton() {
}
private static class SingletonInner{
private static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonInner.instance;
}
//有序列化与反序列化操作,需要调用readResolve方法保证单一实例
protected Object readResolve() {
return SingletonInner.instance;
}
}