单例模式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);//无法输出
}
相关推荐
Java设计模式-单例模式详解
java设计模式中的单例模式,而单例模式又分为饿汉模式和懒汉模式
JAVA-设计模式-创建型模式-单例模式
设计模式--单例模式java例子
Java-设计模式-单例模式-实现源码(简单实现、双重检查锁、静态内部类、枚举类)
设计模式-单例模式 学习时候联系的代码,可以进行参考
常见设计模式-单例模式
本文档,详细的描述了单例模式,有类图,java代码实例,以及讲解、注意点,通过这份文档可以让你很容易理解单例设计模式。
Java面向对象(高级)-- 单例(Singleton)设计模式
Java设计模式-单例模式(懒汉和恶汉)
计算机后端-Java-图解java设计模式037 单例模式JK.avi
java设计模式之单例模式
JAVA设计模式之单例模式。 一篇文章带你快速了解!
入名所示,该文件为最详细的Java单例模式讲解并附有讲解代码。主要讲了单例模式的几种方法,懒汉模式、饿汉模式、静态内部类模式。着重讲解了懒汉模式下怎么实现线程安全。饿汉模式和静态内部类模式如何设置能够避免...
计算机后端-Java-图解java设计模式036 单例(.avi
计算机后端-Java-图解java设计模式038 单例模.avi
计算机后端-Java-图解java设计模式035 单例(静.avi
计算机后端-Java-图解java设计模式034 单例(DoubleC.avi