单例模式


public class DataBaseContext {

    private static DataHelper dataHelper;
    private static Object     INSTANCE_LOCK = new Object();

    public static DataHelper getInstance(Context context) {
    
        synchronized (INSTANCE_LOCK) {
        
           if (dataHelper == null) {
              dataHelper = new DataHelper(context);
            }
        
           return dataHelper;
        }
    }
}

/*  懒汉式单例 */
public class Singleton {
    
    private static Singleton uniqueInstance = null;
    
    private Singleton() {}
    
    public static synchronized Singleton getInstance ( ) {
    
       if ( uniqueInstance == null ) {
           uniqueInstance = new Singleton();
       }
    
       return uniqueInstance;
    }
}


/*  饿汉式单例 */
public class Singleton {
    
    private static Singleton uniqueInstance =  new Singleton();
    
    private Singleton() {}
   
    public static  Singleton getInstance ( ) {
        return uniqueInstance;
    }
}

 

 
posted @ 2012-05-04 10:19  soeyong  阅读(136)  评论(0编辑  收藏  举报