设计模式 单例模式


public class Singleton {

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


public class Program {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Singleton s1=Singleton.GetInstance();
		Singleton s2=Singleton.GetInstance();
		
		if(s1==s2)
		{
			System.out.println("两个对象是相同的实例");
		}
		else
		{
			System.out.println("两个对象是不同实例");
		}
	}

}

posted @ 2016-11-08 22:57  yufenghou  阅读(136)  评论(0)    收藏  举报