A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 考拉是只猫 中级黑马   /  2014-12-10 23:08  /  1647 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.lang.reflect.Constructor;  
  2. /**
  3. * 测试Singleton的可靠性。
  4. *  
  5. */  
  6. public class TestSingleton {  
  7.   public static void main(String[] args) {  
  8.     testSingleton1();  
  9.     testSingleton2();  
  10.     testSingleton3();  
  11.   }  
  12.   public static void testSingleton1() {  
  13.     try {  
  14.       // 测试Singletom1  
  15.       // 拿到第一个实例  
  16.       TestSingleton1 s1 = TestSingleton1.getInstance();  
  17.       // 测试拿到第二个实例  
  18.       Class c1 = Class.forName("TestSingleton1");  
  19.       Constructor[] cons = c1.getDeclaredConstructors();  
  20.       Constructor cc1 = cons[0];  
  21.       cc1.setAccessible(true);  
  22.       TestSingleton1 s2 = (TestSingleton1) cc1.newInstance(null);  
  23.       System.out.println(s1 + "/" + s2);  
  24.       System.out.println(s1 == s2);  
  25.     } catch (Exception ex) {  
  26.       ex.printStackTrace();  
  27.     }  
  28.   }  
  29.   public static void testSingleton2() {  
  30.     try {  
  31.       // 测试Singletom1  
  32.       // 拿到第一个实例  
  33.       TestSingleton2 s1 = TestSingleton2.getInstance();  
  34.       // 测试拿到第二个实例  
  35.       Class c1 = Class.forName("TestSingleton2");  
  36.       Constructor[] cons = c1.getDeclaredConstructors();  
  37.       Constructor cc1 = cons[0];  
  38.       cc1.setAccessible(true);  
  39.       TestSingleton2 s2 = (TestSingleton2) cc1.newInstance(null);  
  40.       System.out.println(s1 + "/" + s2);  
  41.       System.out.println(s1 == s2);  
  42.     } catch (Exception ex) {  
  43.       ex.printStackTrace();  
  44.     }  
  45.   }  
  46.   public static void testSingleton3() {  
  47.     try {  
  48.       // 测试Singletom1  
  49.       // 拿到第一个实例  
  50.       TestSingleton3 s1 = TestSingleton3.getInstance();  
  51.       // 测试拿到第二个实例  
  52.       Class c1 = Class.forName("TestSingleton3");  
  53.       Constructor[] cons = c1.getDeclaredConstructors();  
  54.       Constructor cc1 = cons[0];  
  55.       cc1.setAccessible(true);  
  56.       TestSingleton3 s2 = (TestSingleton3) cc1.newInstance(null);  
  57.       System.out.println(s1 + "/" + s2);  
  58.       System.out.println(s1 == s2);  
  59.     } catch (Exception ex) {  
  60.       ex.printStackTrace();  
  61.     }  
  62.   }  
  63. }  
  64. /**
  65. * 一个普通的Singletone实现。
  66. *  
  67. */  
  68. class TestSingleton1 {  
  69.   private static final TestSingleton1 INSTANCE = new TestSingleton1();  
  70.   public static TestSingleton1 getInstance() {  
  71.     return INSTANCE;  
  72.   }  
  73.   private TestSingleton1() {  
  74.   }  
  75. }  
  76. /**
  77. * 一个用异常强化了的Singletone实现。
  78. *  
  79. */  
  80. class TestSingleton2 {  
  81.   private static final TestSingleton2 INSTANCE = new TestSingleton2();  
  82.   public static TestSingleton2 getInstance() {  
  83.     return INSTANCE;  
  84.   }  
  85.   private static boolean initSign;  
  86.   private TestSingleton2() {  
  87.     if (initSign) {  
  88.       throw new RuntimeException("实例只能建造一次");  
  89.     }  
  90.     initSign = true;  
  91.   }  
  92. }  
  93. /**
  94. * 枚举实现的Singleton
  95. *  
  96. */  
  97. enum TestSingleton3 {  
  98.   INSTANCE;  
  99.   public static TestSingleton3 getInstance() {  
  100.     return INSTANCE;  
  101.   }  
复制代码


1 个回复

倒序浏览
不错,充分说明枚举可以单例
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马