![【设计模式之单例模式InJava】 【设计模式之单例模式InJava】](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
1. 单例模式
1.1饿汉式(开发常用)
class SingleFirst
{
/*
添加其他成员信息
*/
private static SingleFirst s1 = new SingleFirst();
private SingleFirst(){
}
public static SingleFirst getInstance(){
return s1;
}
}
1.2 懒汉式
class SingleSecond
{
/*
添加其他成员信息
*/
private static SingleSecond s2 = null;
private SingleSecond(){
}
public static SingleSecond getInstance(){
if(null==s2)
s2 = new SingleSecond();
return s2;
}
}