zoukankan      html  css  js  c++  java
  • 单例模式

    
    
    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;
        }
    }
     
  • 相关阅读:
    20200816
    20200815
    20200813
    20200811
    20200810
    20200806
    20200804
    20200803
    20200802
    20200801
  • 原文地址:https://www.cnblogs.com/surong/p/2482243.html
Copyright © 2011-2022 走看看