单例模式的测试数据

我用debug模式测试了两种单例模式的执行情况:


第一种:

public class TestSingleTon1 {

	private static TestSingleTon1 singleton1 = null;
	
	public TestSingleTon1(){
		//TODO
		System.out.println("constructor methord...");
		
	}
	
	public static synchronized TestSingleTon1 getInstance(){
	    System.out.println("get instance methord...");
		if(singleton1 == null){
			singleton1 = new TestSingleTon1();
		}
		return singleton1;
	}
}

第二种:

public class TestSingleTon2 {

	private static TestSingleTon2 singleton2 = new TestSingleTon2();
	
	public TestSingleTon2(){
		//TODO
		System.out.println("constructor methord...");
	}
	
	public static TestSingleTon2 getInstance(){
		System.out.println("get instance methord...");
		return singleton2;
	}
}

测试程序:

public class TestMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("=====TestSingleTon1 wrong test=====");
		TestSingleTon1 single1 = new TestSingleTon1();
		System.out.println((single1 == null) + "  " + single1.hashCode());
		TestSingleTon1 single2 = new TestSingleTon1();
		System.out.println((single2 == null) + "  " + single2.hashCode());
		TestSingleTon1 single3 = single2.getInstance();
		System.out.println((single3 == null) + "  " + single3.hashCode());
		
		System.out.println("=====TestSingleTon1 right test=====");
		TestSingleTon1 single1_1 = TestSingleTon1.getInstance();
		System.out.println((single1_1 == null) + "  " + single1_1.hashCode());
		TestSingleTon1 single2_1 = TestSingleTon1.getInstance();
		System.out.println((single2_1 == null) + "  " + single2_1.hashCode());

		System.out.println("=====TestSingleTon2 wrong test=====");
		TestSingleTon2 single4 = new TestSingleTon2();
		System.out.println((single4 == null) + "  " + single4.hashCode());
		TestSingleTon2 single5 = new TestSingleTon2();
		System.out.println((single5 == null) + "  " + single5.hashCode());
		TestSingleTon2 single6 = TestSingleTon2.getInstance();
		System.out.println((single6 == null) + "  " + single6.hashCode());
		
		System.out.println("=====TestSingleTon2 right test=====");
		TestSingleTon2 single4_1 = TestSingleTon2.getInstance();
		System.out.println((single4_1 == null) + "  " + single4_1.hashCode());
		TestSingleTon2 single5_1 = TestSingleTon2.getInstance();
		System.out.println((single5_1 == null) + "  " + single5_1.hashCode());
		
	}

}


测试结果:

=====TestSingleTon1 wrong test=====
constructor methord...
false  1762502123
constructor methord...
false  1193334315
get instance methord...
constructor methord...
false  15735326
=====TestSingleTon1 right test=====
get instance methord...
false  15735326
get instance methord...
false  15735326
=====TestSingleTon2 wrong test=====
constructor methord...
constructor methord...
false  1063563420
constructor methord...
false  130672250
get instance methord...
false  141106670
=====TestSingleTon2 right test=====
get instance methord...
false  141106670
get instance methord...
false  141106670

结果分析:
1.直接使用new,会产生新对象。
2.通过ClassXXX.getInstance,只会找到存在的对象,不存在才会new。

关于第一种的测试情况:

1.      private static TestSingleTon1 singleton1 = null;

1.1.    public TestSingleTon1(){
		//TODO
		System.out.println("constructor methord...");
		
	}

2.      public TestSingleTon1(){
		//TODO
		System.out.println("constructor methord...");
		
	}

3.1.    public static synchronized TestSingleTon1 getInstance(){
	    System.out.println("get instance methord...");
		if(singleton1 == null){
			singleton1 = new TestSingleTon1();
		}
		return singleton1;
	}

3.2.    public TestSingleTon1(){
		//TODO
		System.out.println("constructor methord...");
		
	}

4.      public static synchronized TestSingleTon1 getInstance(){
	    System.out.println("get instance methord...");
		if(singleton1 == null){
			singleton1 = new TestSingleTon1();
		}
		return singleton1;
	}

5.      public static synchronized TestSingleTon1 getInstance(){
	    System.out.println("get instance methord...");
		if(singleton1 == null){
			singleton1 = new TestSingleTon1();
		}
		return singleton1;
	}

关于第二种的测试情况:

6.1.    private static TestSingleTon2 singleton2 = new TestSingleTon2();

6.2.    public TestSingleTon2(){
		//TODO
		System.out.println("constructor methord...");
	}

7.      public TestSingleTon2(){
		//TODO
		System.out.println("constructor methord...");
	}

8.      public TestSingleTon2(){
		//TODO
		System.out.println("constructor methord...");
	}

9.      public static TestSingleTon2 getInstance(){
		System.out.println("get instance methord...");
		return singleton2;
	}

10.     public static TestSingleTon2 getInstance(){
		System.out.println("get instance methord...");
		return singleton2;
	}

如果把wrong test都屏蔽掉,则得到如下结果:

=====TestSingleTon1 wrong test=====
=====TestSingleTon1 right test=====
get instance methord...
constructor methord...
false  148669801
get instance methord...
false  148669801
=====TestSingleTon2 wrong test=====
=====TestSingleTon2 right test=====
constructor methord...
get instance methord...
false  1690552137
get instance methord...
false  1690552137

验证不存在才去new的情况

但!是!要!注!意!

真正的单例模式的构造函数用private声明,而不是上面的public,我测试才用public。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值