单例模式1,饿汉模式
//饿汉式单例类.在类初始化时,已经自行实例化
public class Singleton1 {
//私有的默认构造函数
private Singleton1() {}
//已经自行实例化
private static final Singleton1 single = new Singleton1();
//静态工厂方法
public static Singleton1 getInstance() {
return single;
}
}
单例模式2,懒汉模式
//懒汉式单例类.在第一次调用的时候实例化
public class Singleton2 {
//私有的默认构造函数
private Singleton2() {}
//注意,这里没有final
private static Singleton2 single=null;
//静态工厂方法
public synchronized static Singleton2 getInstance() {
if (single == null) {
single = new Singleton2();
}
return single;
}
}
单例模式3 枚举类型
//可以有效防止通过反射重新创建实例
public class Singleton3 {
//唯一的实例
INSTANCE;
public void doSomething(){
}
}
单例模式4 缓存池模式,IOC模式
import java.util.HashMap;
import java.util.Map;
//将类实例化之后放在静态的Map中缓存,需要时从中获取
public class Singleton4 {
private static Map<String,Singleton3> map = new HashMap<String,Singleton3>();
static{
Singleton4 single = new Singleton4();
map.put(single.getClass().getName(), single);
}
//保护的默认构造函数
protected Singleton4(){}
//静态工厂方法,返还此类惟一的实例
public static Singleton4 getInstance(String name) {
if(name == null) {
name = Singleton4.class.getName();
}
if(map.get(name) == null) {
try {
map.put(name, (Singleton4) Class.forName(name).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return map.get(name);
}
}
@Test
public void aaaaaaaaaaaaaaaaaaaaaaaaa() throws IOException, InterruptedException, InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException {
//普通模式单例,
MyTest2 t1 = MyTest2.getInstance();
MyTest2 t2 = MyTest2.getInstance();
System.out.println(t1 == t2);//输出true
MyTest2 t3 = null;
//通过反射方式访问私有构造方法,创建新实例
Constructor<MyTest2>[] cs1 = MyTest2.class.getDeclaredConstructors();
for (Constructor c : cs1) {
//允许访问私有构造
c.setAccessible(true);
t3 = (MyTest2)c.newInstance();
}
System.out.println(t1 == t3);//输出false
//枚举类型反射创建新实例 ,MyTest1为枚举类
MyTest2 t4 = null;
Constructor<MyTest1>[] cs2 = MyTest1.class.getDeclaredConstructors();
for (Constructor c : cs2) {
//允许访问私有构造
c.setAccessible(true);
//报异常 java.lang.IllegalArgumentException: Cannot reflectively create enum objects
t4 = (MyTest2)c.newInstance();
}
System.out.println(t1 == t4);//无法输出
}
public void aaaaaaaaaaaaaaaaaaaaaaaaa() throws IOException, InterruptedException, InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException {
//普通模式单例,
MyTest2 t1 = MyTest2.getInstance();
MyTest2 t2 = MyTest2.getInstance();
System.out.println(t1 == t2);//输出true
MyTest2 t3 = null;
//通过反射方式访问私有构造方法,创建新实例
Constructor<MyTest2>[] cs1 = MyTest2.class.getDeclaredConstructors();
for (Constructor c : cs1) {
//允许访问私有构造
c.setAccessible(true);
t3 = (MyTest2)c.newInstance();
}
System.out.println(t1 == t3);//输出false
//枚举类型反射创建新实例 ,MyTest1为枚举类
MyTest2 t4 = null;
Constructor<MyTest1>[] cs2 = MyTest1.class.getDeclaredConstructors();
for (Constructor c : cs2) {
//允许访问私有构造
c.setAccessible(true);
//报异常 java.lang.IllegalArgumentException: Cannot reflectively create enum objects
t4 = (MyTest2)c.newInstance();
}
System.out.println(t1 == t4);//无法输出
}