`

[singleton单子模式]

    博客分类:
  • Java
阅读更多
单子模式1:

public class Singleton {
	//1.私有的静态类变量;
	//2.私有的构造子;
	//3.公有的静态工厂方法
	private static Singleton instance = null;

	private Singleton(){}

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

单子模式2:

public class Singleton {
	
	/*
	 1.私有的静态类变量;
	 2.构造子私有;
	 3.公有的静态工厂方法;
	 */
	private static Singleton instance = new Singleton();

	private Singleton() {
	}

	public static Singleton getInstance() {
		return instance;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics