????????????
?????MVBin ???????[ 2017/5/24 9:37:55 ] ??????????????? ????????
????????????Singleton Pattern??
???????????????У?????????????????????????????????????????????????
???????д??????????????
//??????????
public class Singleton {
//.....???????????Щ????
//uniqueInstance?????????????????
private static Singleton uniqueInstance;
//??л????????????????????????????
private Singleton() {
}
//???????getInstance()????????????Singleton.getInstance()?????????
public static Singleton getInstance() {
//uniqueInstance????е?????????????????????????????????????????д???
if(uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
//.......???????????Щ????
}
???????????????ó???????????????????????????д????????????????????????????????辭????????????д??????????????
???????getInstance()?????????????
//??????????
public class Singleton {
//.....???????????Щ????
//uniqueInstance?????????????????
private static Singleton uniqueInstance;
//??л????????????????????????????
private Singleton() {
}
//???????getInstance()????????????Singleton.getInstance()?????????
public static synchronized Singleton getInstance() {
//uniqueInstance????е?????????????????????????????????????????д???
if(uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
//.......???????????Щ????
}
???????synchronized?????????getInstance()???????????????????????????????????????????????????????????????????????????????ε????????????????????????????????????????????δ?????????????????????????д?????????
?????????д???????????
//??????????
public class Singleton {
//.....???????????Щ????
//?????????????????uniqueInstance?????????????????????????JVM?????????????????????????????????????????????????????
private static Singleton uniqueInstance = new Singleton();
//??л????????????????????????????
private Singleton() {
}
//???????getInstance()????????????Singleton.getInstance()?????????
public static Singleton getInstance() {
return uniqueInstance;
}
//.......??????????Щ????
}
????????д????????Ч???????????????????????????????????????????????Ч????£??????????д?????
????????????д???????????
//??????????
public class Singleton {
//.....???????????Щ????
//volatile???????????jdk1.5????汾?????????uniqueInstance???????????????????????????uniqueInstance????
private volatile static Singleton uniqueInstance;
//??л???????
private Singleton() {
}
//????????δ????????????????и÷???
public static Singleton getInstance() {
//????μ??????null
if(uniqueInstance == null) {
//???????
synchronized (Singleton.class) {
//????μ??????null
if(uniqueInstance == null) {
//?????????
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
//.......??????????Щ????
}
????????д?????????????????????jdk1.5?汾???????
???????????д???????????
//???????????????
public enum SingletonEnum {
//????SingletonEnum.INSTANCE???????????????????????????????л?????????????????
INSTANCE;
//...????????
}
???????jdk1.5?汾??????????????????????????????????????????д????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11