/**
*五、
雙重校驗鎖
*/
class
LockSingleton{
private
volatile
static
LockSingleton singleton;
private
LockSingleton(){}
public
static
LockSingleton getInstance(){
if
(singleton==
null
){
synchronized
(LockSingleton.
class
){
if
(singleton==
null
){
singleton=
new
LockSingleton();
}
}
}
return
singleton;
}
}
/**
*
四、枚舉
*/
enum
EnumSingleton{
INSTANCE;
public
void
doSomeThing(){
}
}
/**
*
三、靜態內部類
*/
class
InternalSingleton{
private
static
class
SingletonHolder{
private
final
static
InternalSingleton INSTANCE=
new
InternalSingleton();
}
private
InternalSingleton(){}
public
static
InternalSingleton getInstance(){
return
SingletonHolder.INSTANCE;
}
}
/**
*
二、惡漢
*/
class
HungrySingleton{
private
static
HungrySingleton singleton=
new
HungrySingleton();
private
HungrySingleton(){}
public
static
HungrySingleton getInstance(){
return
singleton;
}
}
/**
*
一、懶漢
*/
class
LazySingleton{
private
static
LazySingleton singleton;
private
LazySingleton(){
}
public
static
LazySingleton getInstance(){
if
(singleton==
null
){
singleton=
new
LazySingleton();
}
return
singleton;
}