class test
{
public static void main(String[] args) {
Single s = Single.getInstance();
System.out.println(s);
Single2 s2 = Single2.getInstance();
System.out.println(s2);
}
}
/*
饿汉模式
*/
class Single
{
public static Single s = new Single();
private Single(){}
public static Single getInstance() {
return s;
}
}
/*
懒汉模式
*/
class Single2
{
public static Single2 s = null;
private Single2(){};
public static Single2 getInstance() {
if (s == null) {
s = new Single2();
}
return s;
}
}
java 设计模式,单例设计模式
最新推荐文章于 2024-06-12 16:46:06 发布