国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

設(shè)計(jì)模式一(單例模式)

這篇具有很好參考價(jià)值的文章主要介紹了設(shè)計(jì)模式一(單例模式)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

主要思路:將構(gòu)造方法私有化,并對外提供一個(gè)static的方法來創(chuàng)建對象

餓漢式單例

public class Hungry {
?
 ? ?private Hungry(){
?
 ?  }
 ? ?private final static Hungry hungry = new Hungry();
 ? ?public static Hungry getInstance(){
 ? ? ? ?return hungry;
 ?  }
?
 ? ?public static void main(String[] args) {
 ? ? ? ?Hungry hungry1 = Hungry.getInstance();
 ? ? ? ?Hungry hungry2 = hungry.getInstance();
 ? ? ? ?System.out.println(hungry1==hungry2);//true
 ?  }
}

缺點(diǎn):一開始就創(chuàng)建對象,占用系統(tǒng)資源

懶漢式單例

public class Lazy {
?
 ? ?private Lazy(){
 ? ? ? ?System.out.println(Thread.currentThread().getName()+"ok");
 ?  }
?
 ? ?private static Lazy lazy;
?
 ? ?public static Lazy getInstance(){
 ? ? ? ?if(lazy==null){
 ? ? ? ? ? ?lazy = new Lazy();
 ? ? ?  }
 ? ? ? ?return lazy;
 ?  }
?
 ? ?public static void main(String[] args) {
 ? ? ? for(int i=0;i<10;i++){
 ? ? ? ? ? new Thread(()->{
 ? ? ? ? ? ? ? Lazy.getInstance();
 ? ? ? ? ? }).start();
 ? ? ? }
 ?  }
}
?

單線程下不會出現(xiàn)問題,但多線程會會有并發(fā)問題,main方法的測試結(jié)果:

Thread-0ok
Thread-2ok
Thread-3ok

會發(fā)生同一時(shí)間創(chuàng)建了多個(gè)對象,所以出現(xiàn)了DCL雙重檢索

DCL懶漢式

public class Lazy {
?
 ? ?private Lazy(){
 ? ? ? ?System.out.println(Thread.currentThread().getName()+"ok");
 ?  }
?
 ? ?private volatile static Lazy lazy; //volatile保證不會出現(xiàn)代碼重排
?
 ? ?public static Lazy getInstance(){
 ? ? ? ?if(lazy==null) {
 ? ? ? ? ? ?synchronized (Lazy.class) {
 ? ? ? ? ? ? ? ?if (lazy == null) {
 ? ? ? ? ? ? ? ? ? ?lazy = new Lazy();
 ? ? ? ? ? ? ? ? ? ?/*
 ? ? ? ? ? ? ? ? ? ? ? ?這個(gè)過程不是一個(gè)原子性,會出現(xiàn)代碼重排現(xiàn)象
 ? ? ? ? ? ? ? ? ? ? ? ?1.開配空間
 ? ? ? ? ? ? ? ? ? ? ? ?2.執(zhí)行構(gòu)造方法
 ? ? ? ? ? ? ? ? ? ? ? ?3.引用執(zhí)行
 ? ? ? ? ? ? ? ? ? ? */
 ? ? ? ? ? ? ?  }
 ? ? ? ? ?  }
 ? ? ?  }
 ? ? ? ?return lazy;
 ?  }
?
 ? ?public static void main(String[] args) {
 ? ? ? for(int i=0;i<10;i++){
 ? ? ? ? ? new Thread(()->{
 ? ? ? ? ? ? ? Lazy.getInstance();
 ? ? ? ? ? }).start();
 ? ? ? }
 ?  }
}

可以實(shí)現(xiàn)延遲實(shí)例化,并且是線程安全的

靜態(tài)內(nèi)部類

public class Holder {
 ? ?private Holder(){
 ? ? ? ?System.out.println(Thread.currentThread().getName()+"ok");
 ?  }
?
 ? ?public static Holder getInstance(){
 ? ? ? ?return InnerClass.holder;
 ?  }
?
 ? ?public static class InnerClass{
 ? ? ? ?private static final Holder holder = new Holder();
 ?  }
?
 ? ?public static void main(String[] args) {
 ? ? ? ?for (int i = 0; i < 10; i++) {
 ? ? ? ? ? ?new Thread(()->{
 ? ? ? ? ? ? ? ?Holder.getInstance();
 ? ? ? ? ?  }).start();
 ? ? ?  }
 ?  }
}

反射破解單例模式

可以采用額外的變量進(jìn)行控制,防止反射

public class Lazy {
?
 ? ?private Lazy(){
 ? ? ? ?synchronized (Lazy.class){
 ? ? ? ? ? ?if(temp == false){
 ? ? ? ? ? ? ? ?temp = true;
 ? ? ? ? ?  }else{
 ? ? ? ? ? ? ? ?throw new RuntimeException("不要用反射破壞單例");
 ? ? ? ? ?  }
 ? ? ?  }
 ?  }
?
 ? ?private volatile static Lazy lazy; //volatile保證不會出現(xiàn)代碼重排
?
 ? ?public static Lazy getInstance(){
 ? ? ? ?if(lazy==null) {
 ? ? ? ? ? ?synchronized (Lazy.class) {
 ? ? ? ? ? ? ? ?if (lazy == null) {
 ? ? ? ? ? ? ? ? ? ?lazy = new Lazy();
 ? ? ? ? ? ? ? ? ? ?/*
 ? ? ? ? ? ? ? ? ? ? ? ?這個(gè)過程不是一個(gè)原子性,會出現(xiàn)代碼重排現(xiàn)象
 ? ? ? ? ? ? ? ? ? ? ? ?1.開配空間
 ? ? ? ? ? ? ? ? ? ? ? ?2.執(zhí)行構(gòu)造方法
 ? ? ? ? ? ? ? ? ? ? ? ?3.引用指向
 ? ? ? ? ? ? ? ? ? ? */
 ? ? ? ? ? ? ?  }
 ? ? ? ? ?  }
 ? ? ?  }
 ? ? ? ?return lazy;
 ?  }
?
 ? ?public static void main(String[] args) throws Exception {
 ? ? ? ?Lazy lazy = Lazy.getInstance();
 ? ? ? ?Constructor<Lazy> declaredConstructor = Lazy.class.getDeclaredConstructor(null);
 ? ? ? ?declaredConstructor.setAccessible(true);
 ? ? ? ?Lazy lazy1 = declaredConstructor.newInstance();
 ? ? ? ?System.out.println(lazy==lazy1);
 ?  }
}

枚舉單例

防止反射破壞

public enum EnumSingleton {
 ? ?INSTANCE;
 ? ?private EnumSingleton() {
?
 ?  }
?
 ? ?public EnumSingleton getInstance(){
 ? ? ? ?return INSTANCE;
 ?  }
?
 ? ?public static void main(String[] args) throws Exception {
 ? ? ? ?EnumSingleton instance = EnumSingleton.INSTANCE;
 ? ? ? ?Constructor<EnumSingleton> declaredConstructor = EnumSingleton.class.getDeclaredConstructor(String.class,int.class);
 ? ? ? ?declaredConstructor.setAccessible(true);
 ? ? ? ?EnumSingleton instance1 = declaredConstructor.newInstance();
 ? ? ? ?System.out.println(instance==instance1);
 ?  }
}

防止反序列化破壞

重寫readResolve()方法文章來源地址http://www.zghlxwxcb.cn/news/detail-817244.html

private Object readResolve() throws ObjectStreamException{
 ? ? ? ?return singleton;
}

到了這里,關(guān)于設(shè)計(jì)模式一(單例模式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 設(shè)計(jì)模式-原型模式

    設(shè)計(jì)模式-原型模式

    目錄 一、傳統(tǒng)方式 二、原型模式 ?三、淺拷貝和深拷貝 克隆羊問題: 現(xiàn)在有一只羊tom,姓名為: tom,年齡為: 1,顏色為: 白色,請編寫程序創(chuàng)建和tom羊?qū)傩酝耆嗤?0只羊。 傳統(tǒng)的方式的優(yōu)缺點(diǎn): 簡單易操作。優(yōu)點(diǎn)是比較好理解。 在創(chuàng)建新的對象時(shí),總是需要重新獲取原

    2024年02月16日
    瀏覽(23)
  • 設(shè)計(jì)模式(6)原型模式

    設(shè)計(jì)模式(6)原型模式

    一、介紹 Java中自帶的原型模式是clone()方法。該方法是Object的方法,native類型。他的作用就是將對象的在內(nèi)存的那一塊內(nèi)存數(shù)據(jù)一字不差地再復(fù)制一個(gè)。我們寫簡單類的時(shí)候只需要實(shí)現(xiàn)Cloneable接口,然后調(diào)用Object::clone方法就可實(shí)現(xiàn)克隆功能。這樣實(shí)現(xiàn)的方式是淺拷貝。 ?1、

    2024年02月12日
    瀏覽(24)
  • 設(shè)計(jì)模式5:原型模式

    Prototype Pattern 如果對象的創(chuàng)建成本比較大,可以基于已有的原型對象通過來創(chuàng)建新的對象,節(jié)省創(chuàng)建時(shí)間。 設(shè)計(jì)模式之原型模式 實(shí)現(xiàn)Cloneable接口的方式。這里就不展開分析淺克隆和深克隆了,后面再專門分析淺克隆和深克隆,不影響對原型模式的理解。 用序列化實(shí)現(xiàn)創(chuàng)建對

    2024年02月11日
    瀏覽(22)
  • 設(shè)計(jì)模式 - 原型模式

    傳統(tǒng)方式 優(yōu)點(diǎn): 比較好理解,簡單易操作 缺點(diǎn): 在創(chuàng)建新的對象時(shí),總是需要重新獲取原始對象的屬性,如果創(chuàng)建的對象比較復(fù)雜 時(shí),效率較低 總是需要重新初始化對象,而不是動態(tài)地獲得對象運(yùn)行時(shí)的狀態(tài), 不夠靈活 原型模式 基本介紹: 原型模式(Prototype模式)是指:用原型

    2024年02月06日
    瀏覽(25)
  • 【設(shè)計(jì)模式】原型模式

    【設(shè)計(jì)模式】原型模式

    原型模式(Prototype Pattern)是用于創(chuàng)建重復(fù)的對象,同時(shí)又能保證性能。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式之一。 這種模式是實(shí)現(xiàn)了一個(gè)原型接口,該接口用于創(chuàng)建當(dāng)前對象的克隆。當(dāng)直接創(chuàng)建對象的代價(jià)比較大時(shí),則采用這種模式。例

    2024年02月13日
    瀏覽(17)
  • 設(shè)計(jì)模式-原型模式詳解

    設(shè)計(jì)模式-原型模式詳解

    簡介設(shè)計(jì)模式 設(shè)計(jì)模式是在軟件開發(fā)中常見問題的解決方案,它們是經(jīng)過實(shí)踐和經(jīng)驗(yàn)總結(jié)出來的可重用的設(shè)計(jì)思想和解決方案。設(shè)計(jì)模式通過提供通用的架構(gòu)、原則和指導(dǎo),幫助開發(fā)人員更有效地編寫高質(zhì)量的代碼。 設(shè)計(jì)模式分為三個(gè)主要類別: 創(chuàng)建型模式:關(guān)注對象的創(chuàng)

    2024年02月10日
    瀏覽(25)
  • 重溫設(shè)計(jì)模式 --- 原型模式

    原型模式 是一種創(chuàng)建型設(shè)計(jì)模式,它允許我們創(chuàng)建一個(gè)對象的副本,而不需要顯式地使用構(gòu)造函數(shù)來創(chuàng)建一個(gè)新的對象。這種模式通常用于創(chuàng)建那些具有復(fù)雜創(chuàng)建過程或需要大量資源的對象。 在原型模式中,我們首先定義一個(gè)原型接口,該接口包含一個(gè)克隆方法。然后我們

    2024年02月13日
    瀏覽(18)
  • 設(shè)計(jì)模式三(原型模式)

    在開發(fā)過程中,創(chuàng)建多個(gè)數(shù)據(jù)相同的對象,每次new都開銷比較大,在這里可以使用對象克隆,以先創(chuàng)建的原型對象為模板進(jìn)行對象的復(fù)制。這種模式是實(shí)現(xiàn)了一個(gè)原型接口,該接口用于創(chuàng)建當(dāng)前對象的克隆。當(dāng)直接創(chuàng)建對象的代價(jià)比較大時(shí),則采用這種模式。例如,一個(gè)對象

    2024年01月25日
    瀏覽(18)
  • 設(shè)計(jì)模式系列-原型模式

    設(shè)計(jì)模式系列-原型模式

    一、上篇回顧 上篇創(chuàng)建者模式中,我們主要講述了創(chuàng)建者的幾類實(shí)現(xiàn)方案,和創(chuàng)建者模式的應(yīng)用的場景和特點(diǎn),創(chuàng)建者模式適合創(chuàng)建復(fù)雜的對象,并且這些對象的每 個(gè)組成部分的詳細(xì)創(chuàng)建步驟可以是動態(tài)的變化的,但是每個(gè)對象的組裝的過程來說可能是相對固定的或者說是

    2024年02月09日
    瀏覽(16)
  • 設(shè)計(jì)模式之原型模式筆記

    設(shè)計(jì)模式之原型模式筆記

    記錄下學(xué)習(xí)設(shè)計(jì)模式-原型模式的寫法。 意圖 :用原型實(shí)例指定創(chuàng)建對象的種類,并且通過復(fù)制這些原型創(chuàng)建新的對象。 結(jié)構(gòu) : 其中: Prototype聲明一個(gè)復(fù)制自身的接口。 ConcretePrototype實(shí)現(xiàn)一個(gè)復(fù)制自身的操作。 Client讓一個(gè)原型復(fù)制自身從而創(chuàng)建一個(gè)新的對象。 適用性: 當(dāng)一個(gè)

    2024年02月10日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包