0x003 單例模式 -- Singleton Pattern

單例模式 -- Singleton Pattern

為了避免一個(gè)全局使用的類重復(fù)的銷毀和創(chuàng)建

一、 最簡單的單例:

  1. 新建一個(gè)單例的類:
package com.lyx.singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public class Singleton {

    private static Singleton singleton=new Singleton();

    private Singleton(){}

    public static Singleton getInstance() {
        return singleton;
    }

    public void sayHello(){
        System.out.println("Hello Singleton");
    }

}

  1. 調(diào)用示例:
package com.lyx.singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public class SingletonPattern {
    public static void main(String args[]){
        Singleton.getInstance().sayHello();
    }
}

二、 深入單例:

  1. 懶漢式-線程不安全
package com.lyx.singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public class Singleton2 {
    private static Singleton2 singleton;
    private Singleton2(){};
    public static Singleton2 getInstance() {
        if (singleton==null){
            singleton=new Singleton2();
        }
        return singleton;
    }
}

  1. 餓漢式-線程安全
package com.lyx.singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public class Singleton3 {

    private static Singleton3 singleton=new Singleton3();

    private Singleton3(){}

    public static synchronized Singleton3  getInstance() {
        return singleton;
    }

    public void sayHello(){
        System.out.println("Hello Singleton");
    }

}

  1. 雙檢鎖/雙重校驗(yàn)鎖
package com.lyx.singleton;
   
   /**
    * Created by FollowWinter on 9/29/16.
    */
   public class Singleton4 {
   
       private volatile static Singleton4 singleton=new Singleton4();
   
       private Singleton4(){}
   
       public static synchronized Singleton4 getInstance() {
           if (singleton == null) {
               synchronized (Singleton4.class) {
                   if (singleton == null) {
                       singleton = new Singleton4();
                   }
               }
           }
           return singleton;
       }
   
       public void sayHello(){
           System.out.println("Hello Singleton");
       }
   
   }

  1. 登記式、內(nèi)部靜態(tài)
package com.lyx.singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public class Singleton4 {

    private static class SingletonHolder {
        private static final Singleton4 INSTANCE = new Singleton4();
    }

    private Singleton4() {
    }

    public static final Singleton4 getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void sayHello() {
        System.out.println("Hello Singleton");
    }

}

  1. 枚舉
package com.lyx.singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public enum Singleton5 {
    INSTANCE;
    public void whateverMethod() {

    }
}

三、 單例模式+工廠模式

這么用是因?yàn)楣S類總是重復(fù)使用,但是沒有太大的操作性


package com.lyx.factory;

import com.lyx.singleton.Singleton;

/**
 * Created by FollowWinter on 9/29/16.
 */
public class SingletonShapFactory {
    private static SingletonShapFactory singletonShapFactory=new SingletonShapFactory();
    private SingletonShapFactory(){};

    public static SingletonShapFactory getInstance() {
        return singletonShapFactory;
    }
}



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容