【单例模式】确保一个类只能有一个实例,并且提供一个全局访问点。
通常单例模式有如下的形式:
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
下面是一个实际的例子,煮咖啡的过程可以用一个类来描述,蓝色部分确保了此类只能被初始化一次,因此不会引起混乱。
public class ChocolateBoiler {
[color=blue][b]private static ChocolateBoiler instance;[/b][/color]
private boolean empty;
private boolean boiled;
[color=blue][b]private ChocolateBoiler(){
empty = true;
boiled = false;
}[/b][/color]
[color=blue][b]public static ChocolateBoiler getInstance(){
if(instance == null){
instance = new ChocolateBoiler();
}
return instance;
}[/b][/color]
public void fill(){
if(isEmpty()){
empty = false;
boiled = false;
}
}
public void drain(){
if(!isEmpty() && isBoiled()){
empty = true;
}
}
public void boil(){
if(!isEmpty() && !isBoiled()){
boiled = true;
}
}
public boolean isEmpty(){
return empty;
}
public boolean isBoiled(){
return boiled;
}
}
上面的代码如果是在单线程的环境下是没有任何问题的,但是当有多个线程访问时,就不能保证在应用中只有一个ChocolateBoiler实例了,因此会产生问题。
如果仅仅是为了保证程序的正确性,只需要添加synchronized关键字就可以了。如下面的方法:
public class ChocolateBoiler {
private static ChocolateBoiler instance;
public static [b][color=blue]synchronized[/color][/b] ChocolateBoiler getInstance(){
if(instance == null){
instance = new ChocolateBoiler();
}
return instance;
}
...
}
上面的方法虽然是线程安全的,但是却减低了并发性,因为当instance不为null时,直接返回就可以,但由于synchronized,必须顺序执行。下面给出了一个优化的方法。(double-check)
public class ChocolateBoiler {
private [b][color=blue]volatile[/color][/b]static ChocolateBoiler instance;
public static ChocolateBoiler getInstance(){
if(instance == null){
[b][color=blue]synchronized(ChocolateBoiler.class)[/color][/b]
{
if(instance == null){
instance = new ChocolateBoiler();
}
}
}
return instance;
}
...
}
通常单例模式有如下的形式:
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
下面是一个实际的例子,煮咖啡的过程可以用一个类来描述,蓝色部分确保了此类只能被初始化一次,因此不会引起混乱。
public class ChocolateBoiler {
[color=blue][b]private static ChocolateBoiler instance;[/b][/color]
private boolean empty;
private boolean boiled;
[color=blue][b]private ChocolateBoiler(){
empty = true;
boiled = false;
}[/b][/color]
[color=blue][b]public static ChocolateBoiler getInstance(){
if(instance == null){
instance = new ChocolateBoiler();
}
return instance;
}[/b][/color]
public void fill(){
if(isEmpty()){
empty = false;
boiled = false;
}
}
public void drain(){
if(!isEmpty() && isBoiled()){
empty = true;
}
}
public void boil(){
if(!isEmpty() && !isBoiled()){
boiled = true;
}
}
public boolean isEmpty(){
return empty;
}
public boolean isBoiled(){
return boiled;
}
}
上面的代码如果是在单线程的环境下是没有任何问题的,但是当有多个线程访问时,就不能保证在应用中只有一个ChocolateBoiler实例了,因此会产生问题。
如果仅仅是为了保证程序的正确性,只需要添加synchronized关键字就可以了。如下面的方法:
public class ChocolateBoiler {
private static ChocolateBoiler instance;
public static [b][color=blue]synchronized[/color][/b] ChocolateBoiler getInstance(){
if(instance == null){
instance = new ChocolateBoiler();
}
return instance;
}
...
}
上面的方法虽然是线程安全的,但是却减低了并发性,因为当instance不为null时,直接返回就可以,但由于synchronized,必须顺序执行。下面给出了一个优化的方法。(double-check)
public class ChocolateBoiler {
private [b][color=blue]volatile[/color][/b]static ChocolateBoiler instance;
public static ChocolateBoiler getInstance(){
if(instance == null){
[b][color=blue]synchronized(ChocolateBoiler.class)[/color][/b]
{
if(instance == null){
instance = new ChocolateBoiler();
}
}
}
return instance;
}
...
}