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

创建型(单例模式)

程序员文章站 2022-07-13 23:44:22
...
package com.lwf.create.singleton;

public class Singleton {
	private Singleton(){}
	private static Singleton st = new Singleton();
	public static Singleton getInstance(){
		return st;
	}
	public static void main(String[] args) {
		
	}
}

class LazySingleton{
	private LazySingleton(){}
	private static LazySingleton st = null;
	public static synchronized LazySingleton getInstance(){
		if(st == null)
			st =  new LazySingleton();
		return st;
	}
}
class Test{
	public static void main(String[] args) {
		Singleton st = Singleton.getInstance();
		LazySingleton lzSt = LazySingleton.getInstance();
	}
}