Singleton 定義
確保每次呼叫都只會有固定自行產生一個實例並提供給整個系統使用
Singleton 使用
- 單例模式可以避免產生多個物件消耗多個資源,如資料庫、IO存取
- 系統參數設定只需要共同一個設定,例如: andorid 中 Context系統內文
Singleton 單例案例
java範例 /** * Name: singleton * Description : To generate onaly one database configuration instane when it is called * Created by blackbryant on 2017/1/7. */ public class DBConfigSinglton { public static final DBConfigSinglton singlton = new DBConfigSinglton("com.mysql.jdbc.Driver","jdbc:mysql://localhost/EMP","jack","123456"); private String className ; private String db_url ; private String user; private String password; private DBConfigSinglton(String className, String db_url, String user, String password){ this.className = className ; this.db_url= db_url ; this.user = user ; this.password = password ; } public static DBConfigSinglton getInstance(){ return singlton ; } }
Singleton Lazy mode
懶惰模式是當第一次呼叫getInstance()才建立實例,而案例一 是系統開啟就建立實例,所以某程度上懶惰模式節省資源的浪 費。懶惰模式呼叫getInstance()前面加上synchronized同步關鍵 字表示當每次呼叫時都需要做同步的檢查,這對效能上有很大 缺點所以不建議使用
/** * Name: singleton * Description : * Created by blackbryant on 2017/1/7. */ public class DBConfigSingltonLazy { private static DBConfigSingltonLazy singlton = null; private String className ; private String db_url ; private String user; private String password; private DBConfigSingltonLazy(String className, String db_url, String user, String password){ this.className = className ; this.db_url= db_url ; this.user = user ; this.password = password ; } public static synchronized DBConfigSingltonLazy getInstance(){ if(singlton==null){ singlton= new DBConfigSingltonLazy("com.mysql.jdbc.Driver","jdbc:mysql://localhost/EMP","jack","123456") } return singlton ; } }
Singleton- Double CheckLock(DCL)
DCL方法可以在需要時建立實例並保持執行緒安全,在呼叫 getInstance()不需要做同步檢查。DCL看起來很完美但是在高並 行的環境下是有可能出現問題或是低於JDK1.5以下
/** * Name: singleton * Description : Singleton double checkLock * Created by blackbryant on 2017/1/7. */ public class DBConfigSingltonDCL { private static DBConfigSingltonDCL singlton = null; private String className ; private String db_url ; private String user; private String password; private DBConfigSingltonDCL(String className, String db_url, String user, String password){ this.className = className ; this.db_url= db_url ; this.user = user ; this.password = password ; } public static DBConfigSingltonDCL getInstance(){ if(singlton==null){ synchronized (DBConfigSingltonDCL.class) { if(singlton==null) singlton = new DBConfigSingltonDCL("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/EMP", "jack", "123456"); } } return singlton ; } }
Singleton- Static Inner Class
當系統載入DBConfigSingltonSIC 不會建立實例,只有在呼叫 getInstance()才會由SingetonHolder開始建立實例,這種方式不 僅物件唯一性、延遲載入也確保執行緒的安全
/** * Name: singleton * Description : Static Inner Class mode * Created by blackbryant on 2017/1/7. */ public class DBConfigSingltonSIC { private static DBConfigSingltonSIC singlton = null; private String className ; private String db_url ; private String user; private String password; private DBConfigSingltonSIC(String className, String db_url, String user, String password){ this.className = className ; this.db_url= db_url ; this.user = user ; this.password = password ; } public static DBConfigSingltonSIC getInstance(){ return SingletonHolder.singleton; } public static class SingletonHolder{ private static final DBConfigSingltonSIC singleton = new DBConfigSingltonSIC("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/EMP", "jack", "123456"); } }
Singleton- Enum
/** * Name: singleton * Description : Static Inner Class mode * Created by blackbryant on 2017/1/7. */ public enum DBConfigSingltonEnum { INSTANCE{ public void DBConfig(){ this.className = "com.mysql.jdbc.Driver" ; this.db_url= "jdbc:mysql://localhost/EMP" ; this.user = "jack" ; this.password = "123456" ; } }; private String className ; private String db_url ; private String user; private String password; public static DBConfigSingltonEnum getInstance(){ return DBConfigSingltonEnum.INSTANCE; } }
留言
張貼留言