Android设计模式之单例模式

一、单例模式的作用

(1) 保持程序运行过程中该类始终只存在一个示例
(2) 对于new性能消耗较大的类,只实例化一次可以提高性能

二、单例模式的实例

1、饿汉

1 public class Singleton {  
2     private static Singleton instance = new Singleton();  
3     private Singleton (){}
4     public static Singleton getInstance() {  
5     return instance;  
6     }  
7 }  

2、懒汉,线程不安全

 1 public class Singleton {  
 2     private static Singleton instance;  
 3     private Singleton (){}   
 4     public static Singleton getInstance() {  
 5     if (instance == null) {  
 6         instance = new Singleton();  
 7     }  
 8     return instance;  
 9     }  
10 }  

3、最好的写法(双重校验锁)

public class Singleton {

    private static volatile Singleton instance = null;

    // private constructor suppresses
    private Singleton(){
    }

    public static Singleton getInstance() {
        // if already inited, no need to get lock everytime
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }

        return instance;
    }
}

其中需要注意的点主要有三点
(1) 私有化构造函数
(2) 定义静态的Singleton instance对象和getInstance()方法
(3) getInstance()方法中需要使用同步锁synchronized (Singleton.class)防止多线程同时进入造成instance被多次实例化

可以看到上面在synchronized (Singleton.class)外又添加了一层if,这是为了在instance已经实例化后下次进入不必执行synchronized (Singleton.class)获取对象锁,从而提高性能。

Ps: 也有实现使用的是private static Object obj = new Object();加上synchronized(obj),实际没有必要多创建一个对象。synchronized(X.class) is used to make sure that there is exactly one Thread in the block.

你可能感兴趣的:(设计模式,android)