【劍指Offer學習】【面試題2 : 實現Singleton 模式——七種實現方式】,offersingleton

來源:互聯網
上載者:User

【劍指Offer學習】【面試題2 : 實現Singleton 模式——七種實現方式】,offersingleton

題目:設計一個類,我們只能產生該類的一個執行個體

public class Test02 {    /**     * 單例模式,懶漢式,安全執行緒     */    public static class Singleton {        private final static Singleton INSTANCE = new Singleton();        private Singleton() {        }        public static Singleton getInstance() {            return INSTANCE;        }    }    /**     * 單例模式,餓漢式,線程不安全     */    public static class Singleton2 {        private static Singleton2 instance = null;        private Singleton2() {        }        public static Singleton2 getInstance() {            if (instance == null) {                instance = new Singleton2();            }            return instance;        }    }    /**     * 單例模式,餓漢式,安全執行緒,多線程環境下效率不高     */    public static class Singleton3 {        private static Singleton3 instance = null;        private Singleton3() {        }        public static synchronized Singleton3 getInstance() {            if (instance == null) {                instance = new Singleton3();            }            return instance;        }    }    /**     * 單例模式,懶漢式,變種,安全執行緒     */    public static class Singleton4 {        private static Singleton4 instance = null;        static {            instance = new Singleton4();        }        private Singleton4() {        }        public static Singleton4 getInstance() {            return instance;        }    }    /**     * 單例模式,使用靜態內部類,安全執行緒【推薦】     */    public static class Singleton5 {        private final static class SingletonHolder {            private static final Singleton5 INSTANCE = new Singleton5();        }        private Singleton5() {        }        public static Singleton5 getInstance() {            return SingletonHolder.INSTANCE;        }    }    /**     * 靜態內部類,使用枚舉方式,安全執行緒【推薦】     */    public enum Singleton6 {        INSTANCE;        public void whateverMethod() {        }    }    /**     * 靜態內部類,使用雙重校正鎖,安全執行緒【推薦】     */    public static class Singleton7 {        private volatile static Singleton7 instance = null;        private Singleton7() {        }        public static Singleton7 getInstance() {            if (instance == null) {                synchronized (Singleton7.class) {                    if (instance == null) {                        instance = new Singleton7();                    }                }            }            return instance;        }    }    public static void main(String[] args) {        System.out.println(Singleton.getInstance() == Singleton.getInstance());        System.out.println(Singleton2.getInstance() == Singleton2.getInstance());        System.out.println(Singleton3.getInstance() == Singleton3.getInstance());        System.out.println(Singleton4.getInstance() == Singleton4.getInstance());        System.out.println(Singleton5.getInstance() == Singleton5.getInstance());        System.out.println(Singleton6.INSTANCE == Singleton6.INSTANCE);        System.out.println(Singleton7.getInstance() == Singleton7.getInstance());    }}

運行結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.