欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

单例模式示例

程序员文章站 2022-07-13 23:37:58
...
package www.demo.Designpattern.单件模式;

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

  

package www.demo.Designpattern.单件模式;

public class Singleton {
	private volatile static Singleton uniqueSingleton;
	private Singleton(){
		
	}
	private static  Singleton getInstance(){
		if (uniqueSingleton==null) {
			synchronized (Singleton.class) {
				if (uniqueSingleton==null) {
					uniqueSingleton = new Singleton();
				}
			}
		}
		return uniqueSingleton;
	}
}