您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么使用Java單例模式”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用Java單例模式”吧!
1 餓漢式(靜態變量)
package com.shi.design.singleton; /** * 單例模式:1 餓漢式(靜態變量) * @author shiye * */ public class Singleton1 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 構造器私有化,外部不能new * 2. 本類內部創建對象實例 * 3. 提供一個共有的靜態方法,返回實力對象 * @author shiye * */ class Singleton{ private Singleton(){} private static final Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } }
2 餓漢式(靜態代碼塊)
package com.shi.design.singleton.type2; /** * 單例模式:2 餓漢式(靜態代碼塊) * @author shiye * */ public class Singleton2 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 構造器私有化,外部不能new * 2. 再靜態代碼塊中創建對象實例 * 3. 提供一個共有的靜態方法,返回實力對象 * @author shiye * */ class Singleton{ private Singleton(){} private static Singleton instance; static { instance= new Singleton(); } public static Singleton getInstance() { return instance; } }
3 懶漢式式(線程不安全)
package com.shi.design.singleton.type3; /** * 單例模式:3 懶漢式式(線程不安全) * @author shiye * */ public class Singleton3 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 構造器私有化,外部不能new * 23. 提供一個共有的靜態方法,再需要的時候去創建找個對象 * @author shiye * */ class Singleton{ private Singleton(){} private static Singleton instance; public static Singleton getInstance() { if(instance == null) { instance= new Singleton(); } return instance; } }
4 懶漢式式(線程安全)
package com.shi.design.singleton.type4; /** * 單例模式:4 懶漢式式(線程安全) * @author shiye * */ public class Singleton4 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 構造器私有化,外部不能new * 23. 提供一個共有的靜態方法,再需要的時候去創建找個對象 (加鎖synchronized 解決線程安全的問題) * @author shiye * */ class Singleton{ private Singleton(){} private static Singleton instance; public static synchronized Singleton getInstance() { if(instance == null) { instance= new Singleton(); } return instance; } }
5 懶漢式式(線程安全,雙重檢查 -推薦使用)
package com.shi.design.singleton.type5; /** * 單例模式:5 懶漢式式(線程安全,雙重檢查) * @author shiye * */ public class Singleton5 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 構造器私有化,外部不能new * 23. 提供一個共有的靜態方法,再需要的時候去創建找個對象 * (加線程代碼塊synchronized 解決線程安全的問題,并且進行雙重檢查) * @author shiye * */ class Singleton{ private Singleton(){} //volatile的變量是說這變量可能會被意想不到地改變,這樣,編譯器就不會去假設這個變量的值了。 private static volatile Singleton instance; public static Singleton getInstance() { if(instance == null) { synchronized (Singleton.class) { if(instance == null) { instance= new Singleton(); } } } return instance; } }
為什么要加 volatile 主要是為了禁止指令重排
6 懶加載式(靜態內部類 推薦使用)
package com.shi.design.singleton.type6; /** * 單例模式:6 懶加載式(靜態內部類) * @author shiye * */ public class Singleton6 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 構造器私有化,外部不能new * 2. 創建一個靜態內部類(Singleton 類裝載到內存中不會裝載SingletonInstance類) * 3. 提供一個getInstance()方法: * 當getInstance()方法被調用的時候才回去實例化SingletonInstance類(裝載類是線程安全的) * @author shiye * */ class Singleton{ private Singleton(){} public static class SingletonInstance{ public static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } }
7 使用枚舉 (推薦使用)
package com.shi.design.singleton.type7; /** * 單例模式:7 使用枚舉 * @author shiye * */ public class Singleton7 { public static void main(String[] args) { Singleton singleton1 = Singleton.INSTANCE; Singleton singleton2 = Singleton.INSTANCE; System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); singleton1.say(); singleton2.say(); } } /** * 使用枚舉 * @author shiye * */ enum Singleton{ INSTANCE; public void say() { System.out.println("hello ~ "); } }
到此,相信大家對“怎么使用Java單例模式”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。