使用反射对单例模式的破坏:
/**
* 反射破坏单例
*/
public class ProxyBreakSingleton {
@Test
public void test(){
//通过单例模式创建对象
DoubleIfSynchronizedSingleton synchronizedSingleton = DoubleIfSynchronizedSingleton.getSingleton();
//反射创建对象
DoubleIfSynchronizedSingleton proxySingleton = ProxyBreakSingleton.getProxySingleton();
System.out.println("hashCode of synchronizedSingleton:"+synchronizedSingleton.hashCode());
System.out.println("hashCode of proxySingleton:"+proxySingleton.hashCode());
System.out.println(synchronizedSingleton == proxySingleton);
}
/**
* 反射获取单例模式对象
*/
private static DoubleIfSynchronizedSingleton getProxySingleton(){
DoubleIfSynchronizedSingleton newSingleton = null;
Constructor<DoubleIfSynchronizedSingleton> constructor = null;
try {
cons