单例模式
饿汉式单例
自己提供实例 构造方法私有 提供静态方法给其他人用
public class Hungry {
private Hungry(){};
private final static Hungry HUNGRY = new Hungry();
public static Hungry getInstance(){
return HUNGRY;
}
}
//静态内部类实现
public class Holder {
private Holder(){};
public static Holder getInstance(){
return InnerClass.HOLDER;
}
public static class InnerClass{
private static final Holder HOLDER = new Holder();
}
}
双重检测锁模式的懒汉式单例 (DCL懒汉式)
public class LazyMan {
private static boolean pei = false ;
private LazyMan ( ) {
synchronized ( LazyMan. class ) {
if ( pei == false ) {
pei = true ;
} else {
throw new RuntimeException ( "不要试图用反射破坏单例异常" ) ;
}
}
}
private volatile static LazyMan lazyMan;
public static LazyMan getInstance ( ) {
if ( lazyMan== null) {
synchronized ( LazyMan. class ) {
if ( lazyMan == null) {
lazyMan = new LazyMan ( ) ;
}
}
}
return lazyMan;
}
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
LazyMan. getInstance ( ) ;
} ) . start ( ) ;
}
Field pei = LazyMan. class . getDeclaredField ( "pei" ) ;
pei. setAccessible ( true ) ;
Constructor< LazyMan> declaredConstructor = LazyMan. class . getDeclaredConstructor ( null) ;
declaredConstructor. setAccessible ( true ) ;
LazyMan instance1 = declaredConstructor. newInstance ( ) ;
pei. set ( instance1, false ) ;
LazyMan instance2 = declaredConstructor. newInstance ( ) ;
System. out. println ( instance1. hashCode ( ) ) ;
System. out. println ( instance2. hashCode ( ) ) ;
}
}
枚举单例(不会被反射破坏)
//枚举本身也是一个Class类
//Cannot reflectively create enum objects
public enum EnumSingleton {
INSTANCE;
public EnumSingleton getInstance(){
return INSTANCE;
}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
EnumSingleton instance1 = EnumSingleton.INSTANCE;
//通过jad反编译,得到EnumSingleton的构造方法,但是还是不能破坏枚举单例
/*private EnumSingleton(String s, int i)
{
super(s, i);
}*/
Constructor<EnumSingleton> declaredConstructor = EnumSingleton.class.getDeclaredConstructor(String.class,int.class);
declaredConstructor.setAccessible(true);
EnumSingleton instance2 = declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
}
}
枚举反编译源码
package com. peidan. singleton;
import java. io. PrintStream;
import java. lang. reflect. Constructor;
import java. lang. reflect. InvocationTargetException;
public final class EnumSingleton extends Enum
{
public static EnumSingleton[ ] values ( )
{
return ( EnumSingleton[ ] ) $VALUES. clone ( ) ;
}
public static EnumSingleton valueOf ( String name)
{
return ( EnumSingleton) Enum. valueOf ( com/ peidan/ singleton/ EnumSingleton, name) ;
}
private EnumSingleton ( String s, int i)
{
super ( s, i) ;
}
public EnumSingleton getInstance ( )
{
return INSTANCE;
}
public static void main ( String args[ ] )
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
{
EnumSingleton instance1 = INSTANCE;
Constructor declaredConstructor = com/ peidan/ singleton/ EnumSingleton. getDeclaredConstructor ( null) ;
declaredConstructor. setAccessible ( true ) ;
EnumSingleton instance2 = ( EnumSingleton) declaredConstructor. newInstance ( new Object [ 0 ] ) ;
System. out. println ( instance1) ;
System. out. println ( instance2) ;
}
public static final EnumSingleton INSTANCE;
private static final EnumSingleton $VALUES[ ] ;
static
{
INSTANCE = new EnumSingleton ( "INSTANCE" , 0 ) ;
$VALUES = ( new EnumSingleton [ ] {
INSTANCE
} ) ;
}
}