單例模式的幾種實現--《java開發技術-在架構中體驗設計模式和演算法之美》

來源:互聯網
上載者:User

標籤:

package com.doctor.java.design_pattern;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 單例模式的幾種實現--《java開發技術-在架構中體驗設計模式和演算法之美》 *  * @author doctor * * @time 2015年4月24日 下午11:11:03 */public class SingletonPattern {/** * @param args */public static void main(String[] args) {LazySingleton.getInstance();LazySingleton.getInstance();LazySingleton.getInstance();//EagerSingleton.getInstance();EagerSingleton.getInstance();//DoubleCheckLazySingleton.getInstance();DoubleCheckLazySingleton.getInstance();DoubleCheckLazySingleton.getInstance();//Singleton.getInstance();Singleton.getInstance();//EnumSingleton.instance.EnumSingletonOperation();EnumSingleton.instance.EnumSingletonOperation();}/** * 懶漢式單例,安全執行緒 */public static class LazySingleton {private static final Logger log = LoggerFactory.getLogger(LazySingleton.class);// 私人靜態對象,載入時候不做初始化private static LazySingleton singleton = null;// 私人構造方法,避免外部建立執行個體private LazySingleton() {log.info(LazySingleton.class.getName() + "單例 建構函式調用");}/** * 靜態Factory 方法,返回此類的唯一執行個體 當發現執行個體沒有初始化的時候,才初始化 *  * @return */public static synchronized LazySingleton getInstance() {if (singleton == null) {singleton = new LazySingleton();}return singleton;}}/** * 懶漢式單例,安全執行緒,雙重加鎖提高訪問速度 */public static class DoubleCheckLazySingleton {private static final Logger log = LoggerFactory.getLogger(DoubleCheckLazySingleton.class);// 私人靜態對象,載入時候不做初始化private static DoubleCheckLazySingleton singleton = null;// 私人構造方法,避免外部建立執行個體private DoubleCheckLazySingleton() {log.info(DoubleCheckLazySingleton.class.getName() + "單例 建構函式調用");}/** * 靜態Factory 方法,返回此類的唯一執行個體 當發現執行個體沒有初始化的時候,才初始化 *  * @return */public static DoubleCheckLazySingleton getInstance() {if (singleton == null) {synchronized (DoubleCheckLazySingleton.class) {if (singleton == null) {singleton = new DoubleCheckLazySingleton();}}}return singleton;}}/** * 餓漢式單例 */private static class EagerSingleton {private static final Logger log = LoggerFactory.getLogger(EagerSingleton.class);private static final EagerSingleton instance = new EagerSingleton();private EagerSingleton() {log.info(EagerSingleton.class.getName() + "單例 建構函式調用");}public static EagerSingleton getInstance() {return instance;}}/** * 消極式載入+安全執行緒=內部類+靜態初始化多線程預設同步鎖 */public static class Singleton {private static final Logger log = LoggerFactory.getLogger(Singleton.class);private Singleton() {log.info(Singleton.class.getName() + "單例 建構函式調用");}public static Singleton getInstance() {return SingletonHolder.instance;}/** * 類級的內部類,也就是靜態成員內部類,該內部類的執行個體與外部類的執行個體沒有綁定關係,而且只有被調用到才會裝載,而從實現 了消極式載入 */private static class SingletonHolder {// 靜態初始化,由JVM來保證安全執行緒private static Singleton instance = new Singleton();}}/** * 枚舉實現單一實例控制,提供序列化,且由jvm保證安全 */public static enum EnumSingleton {// 定義一個枚舉元素,它只代表了EnumSingleton的一個執行個體。instance;/** * 執行個體方法,單例可以有自己的操作 */public void EnumSingletonOperation() {// 功能處理}}}


單例模式的幾種實現--《java開發技術-在架構中體驗設計模式和演算法之美》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.