单例模式示例

时间:2022-11-30 20:50:30
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;
	}
}