java中单例模式的3种实现
?1 餓漢式單例類.在類初始化時(shí),已經(jīng)自行實(shí)例化?
class EagerSingleton {????
? private static final EagerSingleton m_instance = new EagerSingleton();????
???
? /** * 私有的默認(rèn)構(gòu)造子 */???
? private EagerSingleton() {????
?? }????
???
? /**?
??? * * 靜態(tài)工廠方法?
??? */???
? public static EagerSingleton getInstance() {????
??? return m_instance;????
?? }????
}???
2 懶漢式單例類.在第一次調(diào)用的時(shí)候?qū)嵗?/p>
class LazySingleton {????
? // 注意,這里沒有final????
? private static LazySingleton m_instance = null;????
???
? /** * 私有的默認(rèn)構(gòu)造子 */???
? private LazySingleton() {????
?? }????
???
? /**?
??? * * 靜態(tài)工廠方法?
??? */???
? public synchronized static LazySingleton getInstance() {????
??? if (m_instance == null) {????
?????? m_instance = new LazySingleton();????
???? }????
??? return m_instance;????
?? }????
}???
在上面給出懶漢式單例類實(shí)現(xiàn)里對(duì)靜態(tài)工廠方法使用了同步化,以處理多線程環(huán)境。有些設(shè)計(jì)師在這里
建議使用所謂的"雙重檢查成例".必須指出的是,"雙重檢查成例"不可以在Java 語言中使用。不十分熟
悉的讀者,可以看看后面給出的小節(jié)。
同樣,由于構(gòu)造子是私有的,因此,此類不能被繼承。餓漢式單例類在自己被加載時(shí)就將自己實(shí)例
化。即便加載器是靜態(tài)的,在餓漢式單例類被加載時(shí)仍會(huì)將自己實(shí)例化。單從資源利用效率角度來講,
這個(gè)比懶漢式單例類稍差些。
從速度和反應(yīng)時(shí)間角度來講,則比懶漢式單例類稍好些。然而,懶漢式單例類在實(shí)例化時(shí),必須處
理好在多個(gè)線程同時(shí)首次引用此類時(shí)的訪問限制問題,特別是當(dāng)單例類作為資源控制器,在實(shí)例化時(shí)必
然涉及資源初始化,而資源初始化很有可能耗費(fèi)時(shí)間。這意味著出現(xiàn)多線程同時(shí)首次引用此類的機(jī)率變
得較大。
餓漢式單例類可以在Java 語言內(nèi)實(shí)現(xiàn), 但不易在C++ 內(nèi)實(shí)現(xiàn),因?yàn)殪o態(tài)初始化在C++ 里沒有固定
的順序,因而靜態(tài)的m_instance 變量的初始化與類的加載順序沒有保證,可能會(huì)出問題。這就是為什么
GoF 在提出單例類的概念時(shí),舉的例子是懶漢式的。他們的書影響之大,以致Java 語言中單例類的例子
也大多是懶漢式的。實(shí)際上,本書認(rèn)為餓漢式單例類更符合Java 語言本身的特點(diǎn)。
?
3 登記式單例類.類似Spring里面的方法,將類名注冊(cè),下次從里面直接獲取。 查看復(fù)制到剪切板打印
import java.util.HashMap;????
???
class RegSingleton {????
? static private HashMap m_registry = new HashMap();????
? static {????
???? RegSingleton x = new RegSingleton();????
???? m_registry.put(x.getClass().getName(), x);????
?? }????
???
? /** * 保護(hù)的默認(rèn)構(gòu)造子 */???
? protected RegSingleton() {????
?? }????
???
? /** * 靜態(tài)工廠方法,返還此類惟一的實(shí)例 */???
? static public RegSingleton getInstance(String name) {????
??? if (name == null) {????
?????? name = RegSingleton.class.getName();????
???? }????
??? if (m_registry.get(name) == null) {????
????? try {????
???????? m_registry.put(name, Class.forName(name).newInstance());????
?????? } catch (Exception e) {????
???????? System.out.println("Error happened.");????
?????? }????
????? return (RegSingleton) (m_registry.get(name));????
???? }????
??? return null;????
?? }????
???
? /** * 一個(gè)示意性的商業(yè)方法 */???
? public String about() {????
??? return "Hello, I am RegSingleton.";????
?? }????
}????
???
class RegSingletonChild extends RegSingleton {????
? public RegSingletonChild() {????
?? }????
???
? /** * 靜態(tài)工廠方法 */???
? static public RegSingletonChild getInstance() {????
??? return (RegSingletonChild) RegSingleton.getInstance
("com.javapatterns.singleton.demos.RegSingletonChild");????
?? }????
???
? /** * 一個(gè)示意性的商業(yè)方法 */???
? public String about() {????
??? return "Hello, I am RegSingletonChild.";????
?? }????
}???
?
http://langfangwangbin.blog.163.com/blog/static/1005436342008112711449788/
?
轉(zhuǎn)載于:https://blog.51cto.com/herculeser/1165045
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java中单例模式的3种实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到一个人预示着什么
- 下一篇: Functional Programmi