Java-設計模式-單例模式-餓漢模式、懶漢模式

來源:互聯網
上載者:User

標籤:總結   資料不一致   rdo   設計模式   成員   佔用   構造方法   原理   日誌   

//-------------------------------------------------------------餓漢模式--開始-----------------------------------------------------------package com.study.DesignPattern01;/** * 建立一個餓漢模式的單例 * @author ZLHome *有些對象,我們只需要一個,如果多了,那麼就可能導致資料不一致,    佔用資源過多等等,比如:        設定檔、工具類、線程池、緩衝、日誌對象 */public class Singleton {    //1、構造方法私人化(這樣類就不能被執行個體化了)    private Singleton(){        System.out.println("執行個體化Singleton類");    }        //2、執行個體化單例對象,對象為靜態(這樣就只會執行個體化一次),私人的(安全,外部不能直接Singleton.instance調用)    private static Singleton instance = new Singleton();        //3、提供一個靜態方法(靜態方法,類可以直接調用),用於擷取單例    public static Singleton getInstance(){        return instance;    }}//---------------------------------------------------------------------------------------------package com.study.DesignPattern01;import org.junit.After;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;public class SingletonTest {    @BeforeClass    public static void setUpBeforeClass() throws Exception {        System.out.println("Junit開始BeforeClass...");    }    @AfterClass    public static void tearDownAfterClass() throws Exception {        System.out.println("Junit過後AfterClass...");    }    @Before    public void setUp() throws Exception {        System.out.println("Junit開始Before...");    }    @After    public void tearDown() throws Exception {        System.out.println("Junit過後After...");    }    @Test    public void test() {        System.out.println("Junit開始...");        Singleton instance = Singleton.getInstance();        Singleton instance1 = Singleton.getInstance();        System.out.println("是否相等:"+(instance==instance1));    }}//-------------------------------------------------------------餓漢模式--結束-----------------------------------------------------------//============================================================懶漢模式--開始============================================================package com.study.DesignPattern01;public class Singleton1 {    //1、將構造方法設定為私人(這樣類就不能被外部執行個體化了)    private Singleton1(){        System.out.println("執行個體化Singleton1");    }        //2、申明單例對象    private static Singleton1 instance;        //3、提供一個靜態方法(靜態方法屬於類),用於外部調用    public static Singleton1 getInstance(){        if(instance==null){            System.out.println("第一次執行個體化Singleton1對象");            instance=new Singleton1();        }        return instance;    }}//============================================================package com.study.DesignPattern01;import org.junit.After;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;public class Singleton1Test {    @BeforeClass    public static void setUpBeforeClass() throws Exception {        System.out.println("Junit開始BeforeClass...");    }    @AfterClass    public static void tearDownAfterClass() throws Exception {        System.out.println("Junit過後AfterClass...");    }    @Before    public void setUp() throws Exception {        System.out.println("Junit開始Before...");    }    @After    public void tearDown() throws Exception {        System.out.println("Junit過後After...");    }    @Test    public void test() {        System.out.println("Junit開始...");        Singleton1 instance = Singleton1.getInstance();        Singleton1 instance1 = Singleton1.getInstance();        System.out.println("是否相等:"+(instance==instance1));    }}//============================================================懶漢模式--結束============================================================

 

設計模式
可靠性更高、更容易理解、擴充性更好‘更容易維護
1、單例模式:
1)單例背景、情況:
有些對象,我們只需要一個,如果多了,那麼就可能導致資料不一致,
佔用資源過多等等,比如:
設定檔、工具類、線程池、緩衝、日誌對象
2)原理:
執行個體化對象是通過構造方法來實現的(程式類未寫,則程式類有預設的構造方法),
單例只允許擷取一個執行個體,所以

餓漢模式:
類載入的時候就執行個體化對象(比較餓,主動執行個體對象)
(1)實現單例就去改寫構造方法:將構造方法改寫為私人的,改寫之後,就不能直接執行個體化了(不允許外部直接建立執行個體)
(2)建立類的唯一執行個體(類裡面去new一個執行個體,且這個執行個體是static[變數、方法加static後就變成類所有,不是必須建立執行個體對象來調用],這樣就可以通過類直接調用這個執行個體[類名.執行個體變數名])
(3)為了安全,不允許外部直接存取成員變數,所以(2)需要最佳化,將類的static執行個體變為private,變為private之後就不能直接通過"類名.執行個體變數名"來訪問了,所以提供類的方法get,類的get方法也需要是static才能通過"類名.方法名"調用

懶漢模式:
類載入的時候只是聲明一下,調用這個單例的時候才執行個體化(比較懶,不主動去執行個體對象)
(1)先聲明一個單例,並不執行個體化單例對象
(2)在get方法裡面去判斷,如果沒有執行個體這個對象,就將單例對象執行個體化再返回

對比:
懶漢模式:類載入快,擷取對象慢,安全執行緒的
餓漢模式:類載入慢,擷取對象快,線程不安全

總結:static修飾的變數、方法資料類,可以通過類直接調用

Java-設計模式-單例模式-餓漢模式、懶漢模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.