设计模式单例模式
流殃了解一下
核心作用
保证一个类只有一个实例,并且提供一个访问该实例的全局访问点
常见的五种单例模式的是实现方式
- 饿汉式(线程安全,调用效率高,不能延时加载)
- 懒汉式(线程安全,调用效率不高,可以延时加载)
- DCL懒汉式(由于JVM底层内部模型原因,偶尔会出现问题,不推荐使用)
- 饿汉式改进(静态内部类,线程安全,调用效率高,可以延时加载)
- 枚举单例(线程安全,调用效率高,不能延时加载)
饿汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class demo1 { private demo1(){ }
private static demo1 instance=new demo1(); public static demo1 getInstance(){ return instance; } }
class demo1Test{ public static void main(String[] args) { demo1 instance=demo1.getInstance(); demo1 instance1=demo1.getInstance(); System.out.println(instance==instance1); } }
|
懒汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class demo2 { private demo2(){ } private static demo2 instance; public static synchronized demo2 getInstance(){ if(instance==null) { instance=new demo2(); } return instance; } } class demo2Test{ public static void main(String[] args) { demo2 instance=demo2.getInstance(); demo2 instance1=demo2.getInstance(); System.out.println(instance==instance1); } }
|
DCL懒汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class demo3 { private demo3(){ } private volatile static demo3 instance; public static demo3 getInstance(){ if(instance==null) { synchronized (demo3.class){ if(instance==null) { instance=new demo3(); } } } return instance; } }
class demo3Test{ public static void main(String[] args) { demo3 instance=demo3.getInstance(); demo3 instance1=demo3.getInstance(); System.out.println(instance==instance1); } }
|
静态内部类实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class demo4 { private demo4(){ } private static class InnerClass{ private static final demo4 instance=new demo4(); } public static demo4 getInstance(){ return InnerClass.instance; } }
class demo4Test{ public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { demo4 instance=demo4.getInstance(); Constructor<demo4> demo4Constructor=demo4.class.getDeclaredConstructor(null); demo4Constructor.setAccessible(true); demo4 instance1=demo4Constructor.newInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } }
|
优化静态内部类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class demo5 { private demo5(){ synchronized (demo5.class){ if(instance!=null) { throw new RuntimeException("不要试图用反射破坏单例模式!"); } } } private volatile static demo5 instance; public static demo5 getInstance(){ if(instance==null) { synchronized (demo5.class){ if(instance==null) { instance=new demo5(); } } } return instance; } } class demo5Test{ public static void main(String[] args) throws Exception { demo5 instance=demo5.getInstance(); Constructor<demo5> demo4Constructor=demo5.class.getDeclaredConstructor(null); demo4Constructor.setAccessible(true); demo5 instance1=demo4Constructor.newInstance(); System.out.println(instance==instance1); System.out.println(instance.hashCode()); System.out.println(instance1.hashCode()); } }
|
枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public enum demo6 { INTERFACE; public demo6 getInstance(){ return INTERFACE; } } class demo6Test{ public static void main(String[] args) { demo6 anInterface = demo6.INTERFACE; demo6 anInterface2 = demo6.INTERFACE; System.out.println(anInterface==anInterface2); } }
|