单例模式

推荐使用方法:

public static class Singleton{
		private static final Singleton instance = new Singleton();
		
		private Singleton(){
			
		}
		
		public static Singleton getInstance(){
			return instance;
		}
	}

  线程不安全:(加上synchronized也不够好):

public static class Singleton{
		private static Singleton instance = null;
		
		private Singleton(){
			
		}
		
		public static Singleton getInstance(){
			if(instance == null){
				instance = new Singleton();
			}
			return instance;
		}
	}

  

posted on 2015-10-08 11:29  lastView  阅读(95)  评论(0)    收藏  举报

导航