類的單例設(shè)計(jì)模式,就是采取一定的方法保證在整個(gè)的軟件系統(tǒng)中,對某個(gè)類只能存在一個(gè)對象實(shí)例
,并且該類只提供一個(gè)取得其對象實(shí)例的方法(靜態(tài)方法)
指一個(gè)類只有一個(gè)實(shí)例,且該類能自行創(chuàng)建這個(gè)實(shí)例的一種模式
。
特點(diǎn):
某個(gè)類只能有一個(gè)實(shí)例(即構(gòu)造器私有,防止外部通過new XXX()創(chuàng)建對象
)
自行在類的內(nèi)部創(chuàng)建對象實(shí)例
向外暴露一個(gè)靜態(tài)的公共方法
八種單例模式實(shí)現(xiàn)方式:
-
餓漢式(靜態(tài)常量)
-
餓漢式(靜態(tài)代碼塊)
-
懶漢式(線程不安全)
-
懶漢式(線程安全,同步方法)
-
懶漢式(線程安全,同步代碼塊)
-
雙重檢查
-
靜態(tài)內(nèi)部類
-
枚舉
餓漢式(靜態(tài)常量)
public class Singleton {
//創(chuàng)建實(shí)例對象
private final static Singleton instance = new Singleton();
/**
* 構(gòu)造器私有化,防止外部類能直接new
*/
private Singleton() {}
/**
* 提供一個(gè)公有的靜態(tài)方法,返回實(shí)例對象
* @return
*/
public static Singleton getInstance() {
return instance;
}
}
類裝載的時(shí)候就完成實(shí)例化,避免了線程同步問題,如果從始至終都沒有用到這個(gè)實(shí)例,則會造成內(nèi)存的浪費(fèi)
餓漢式(靜態(tài)代碼塊)
public class Singleton {
//實(shí)例對象
private static Singleton instance;
/**
* 在靜態(tài)代碼塊中,創(chuàng)建單例對象
*/
static {
instance = new Singleton();
}
/**
* 構(gòu)造器私有化,防止外部類能直接new
*/
private Singleton() {}
/**
* 提供一個(gè)公有的靜態(tài)方法,返回實(shí)例對象
* @return
*/
public static Singleton getInstance() {
return instance;
}
}
同上
懶漢式(線程不安全)
當(dāng)需要使用到對象的時(shí)候,才會創(chuàng)建對象即懶漢式
public class Singleton {
//實(shí)例對象
private static Singleton instance;
/**
* 構(gòu)造器私有化,防止外部類能直接new
*/
private Singleton() {}
/**
* 提供一個(gè)公有的靜態(tài)方法,當(dāng)使用到該方法時(shí),才會創(chuàng)建instance
* @return
*/
public static Singleton getInstance() {
//若對象為空,則創(chuàng)建
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
起到了懶加載的效果,但是只能在單線程下使用,在實(shí)際開發(fā)中,不要使用這種方式
懶漢式(線程安全,同步方法)
public class YamlUtils {
private static YamlUtils instance; // 類實(shí)例變量
private YamlUtils() {
loadConfig(); // 加載配置信息
}
/**
* 提供一個(gè)公有的靜態(tài)方法,加入同步處理,解決線程安全問題
* @return
*/
public static synchronized YamlUtils getInstance() {
if (instance == null) { // 若實(shí)例為空,則創(chuàng)建新實(shí)例
instance = new YamlUtils();
}
return instance;
}
private void loadConfig() {
}
}
效率低,每個(gè)線程在想獲得類的實(shí)例時(shí)候,執(zhí)行 getInstance()方法都要進(jìn)行同步,而其實(shí)這個(gè)方法只執(zhí)行一次實(shí)例化代碼就夠了,后面的想獲得該類的實(shí)例,直接 return 就行,方法進(jìn)行同步效率太低
在實(shí)際開發(fā)中,不推薦使用
懶漢式(線程安全,同步代碼塊)
class Singleton {
//實(shí)例對象
private static Singleton instance;
/**
* 構(gòu)造器私有化,防止外部類能直接new
*/
private Singleton() {}
/**
* 提供一個(gè)公有的靜態(tài)方法
* @return
*/
public static Singleton getInstance() {
//若對象為空,則創(chuàng)建
if (instance == null) {
//同步代碼塊
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return instance;
}
}
因?yàn)樯厦鎸?shí)現(xiàn)方式的同步方法效率太低,改為同步產(chǎn)生實(shí)例化的代碼塊
同步并不能起到線程同步的作用,假如一個(gè)線程進(jìn)入了判斷語句,還沒有執(zhí)行對象實(shí)例化,另一個(gè)線程也通過了這個(gè)判斷語句,這是便會產(chǎn)生多個(gè)實(shí)例
實(shí)際開發(fā)中,不能使用這種方式
雙重檢查 ??
public class Singleton {
//實(shí)例對象
private static volatile Singleton instance;
/**
* 構(gòu)造器私有化,防止外部類能直接new
*/
private Singleton() {}
/**
* 提供一個(gè)公有的靜態(tài)方法,加入雙重檢查處理,解決線程安全問題,
* 同時(shí)解決懶加載問題,也保證了效率
* @return
*/
public static Singleton getInstance() {
//實(shí)例沒創(chuàng)建,才會進(jìn)入內(nèi)部的 synchronized代碼塊
if (instance == null) {
synchronized (Singleton.class) {
//此處再判斷一次,可能會出現(xiàn)其他線程創(chuàng)建了實(shí)例
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
1、DoubleCheck 概念是多線程開發(fā)中常使用到的,如代碼中所示,進(jìn)行了兩次 if (instance == null)檢查,這樣就保證了線程安全。
2、實(shí)例化代碼只用執(zhí)行一次,后面再次訪問時(shí),判斷 if (instance == null),直接 return 實(shí)例化對象,也避免反復(fù)進(jìn)行方法同步。
3、線程安全,延遲加載,效率較高。
4、推薦使用這種單例設(shè)計(jì)模式。
靜態(tài)內(nèi)部類 ??
public class Singleton {
private Singleton() {}
/**
* 靜態(tài)內(nèi)部類,類中有一個(gè)靜態(tài)屬性
*/
private static class SingletonInstance {
private static final Singleton INSTANCE = new Singleton();
}
public static final Singleton getInstance() {
return SingletonInstance.INSTANCE ;
}
}
1、這種方式采用了類裝載的機(jī)制來保證初始化實(shí)例時(shí)只有一個(gè)線程。
2、靜態(tài)內(nèi)部類方式在 Singleton 類被裝載時(shí)并不會立即實(shí)例化,而是在需要實(shí)例化時(shí),調(diào)用 getInstance 方法時(shí),才會裝載 SingletonInstance 類,從而完成 Singleton 的實(shí)例化。
3、類的靜態(tài)屬性只會在第一次加載類的時(shí)候初始化,所以在這里,JVM 幫助我們保證了線程的安全性,在類進(jìn)行初始化時(shí),別的線程是無法進(jìn)入的。
4、避免了線程不安全,利用靜態(tài)內(nèi)部類特點(diǎn)實(shí)現(xiàn)延遲加載,效率高,推薦使用。
枚舉實(shí)現(xiàn)單例模式
public enum Singleton{
INSTANCE;
public Singleton getInstance(){
return INSTANCE;
}
}
public class SingletonTest01 {
public static void main(String[] args) {
Singleton instance1 = Singleton.INSTANCE.getInstance();
Singleton instance2 = Singleton.INSTANCE.getInstance();
System.out.println(instance1 == instance2);
System.out.println("instance1=" + instance1.hashCode());
System.out.println("instance2=" + instance2.hashCode());
}
}
1、借助 JDK1.5 中添加的枚舉來實(shí)現(xiàn)單例模式。不僅能避免多線程同步問題,而且還能防止反序列化重新創(chuàng)建新的對象
推薦使用文章來源:http://www.zghlxwxcb.cn/news/detail-807663.html
總結(jié)
1、單例模式保證了系統(tǒng)內(nèi)存中該類只存在一個(gè)對象,節(jié)省了系統(tǒng)資源,對于一些需要頻繁創(chuàng)建銷毀的對象,使用單例模式可以提高系統(tǒng)性能。
2、當(dāng)想實(shí)例化一個(gè)單例類的時(shí)候,必須要記住使用相應(yīng)的獲取對象的方法,而不是使用 new。
3、單例模式使用的場景:
需要頻繁的進(jìn)行創(chuàng)建和銷毀的對象、創(chuàng)建對象時(shí)耗時(shí)過多或耗費(fèi)資源過多(即:重量級對象),但又經(jīng)常用到的對象、工具類對象、頻繁訪問數(shù)據(jù)庫或文件的對象(如:數(shù)據(jù)源、session 工廠等)文章來源地址http://www.zghlxwxcb.cn/news/detail-807663.html
到了這里,關(guān)于【設(shè)計(jì)模式 創(chuàng)建型】單例模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!