https://juejin.cn/post/7325069743143157801
单例模式旨在确保一个类只有一个实例,并提供一个全局访问点。然而,有几种方法可以破坏单例模式:
Cloneable
接口,通过克隆方法可以创建额外的实例。public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
if (INSTANCE != null) {
throw new IllegalStateException("Instance already created!");
}
}
public static Singleton getInstance() {
return INSTANCE;
}
}