使用單例時的三種單例寫法,使用三種寫法

來源:互聯網
上載者:User

使用單例時的三種單例寫法,使用三種寫法
單例:一個類只有一個執行個體,在外部建立對象時,不能用alloc.(只要alloc,就會在堆區開闢空間,就意味著有多個對象)所以我們要提供一個建立對象的方法:
1.加號方法

2.返回值類型為當前類

3.方法名以default ,standared,main,shared等開頭 + 當前類名


下面以Person類為例

在.h檔案中聲明

+ (Person *)sharePerson;


在.m檔案實現
第一種模式(單線程使用這種模式)

+ (Person *)sharePerson {   

聲明為static,保證變數在程式運行期間不會回收,而且只保證初始化一次   

單例的空間在程式的運行期間不回收,要謹慎使用,否則會造成記憶體堆積 

  static Person *person =nil;   

  if (!person) { 

     person = [[Person alloc]init];   

}   

     return person;

}


第二種模式(多線程使用這種模式)

+ (Person *)sharePerson {    

多線程寫法   

    staticPerson *person =nil;   

    @synchronized(self)    {       

    if (person ==nil) {           

        person = [[Personalloc]init];       

    }       

}   

        return person;

}


第三種模式(單線程與多線程均可使用這種模式)

+ (Person *)sharePerson {       

    staticPerson *person =nil;   

    staticdispatch_once_t onceToken;   

      dispatch_once(&onceToken, ^{      

      保證只執行一次,不管是多線程,還是單線程      

       person = [[Personalloc]init];   

         });       

return person;

}    


java中的單例模式的代碼怎寫

我從我的部落格裡把我的文章粘貼過來吧,對於單例模式模式應該有比較清楚的解釋:
單例模式在我們日常的項目中十分常見,當我們在項目中需要一個這樣的一個對象,這個對象在記憶體中只能有一個執行個體,這時我們就需要用到單例。

一般說來,單例模式通常有以下幾種:

1.饑漢式單例

public class Singleton {
private Singleton(){};
private static Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
}

這是最簡單的單例,這種單例最常見,也很可靠!它有個唯一的缺點就是無法完成消極式載入——即當系統還沒有用到此單例時,單例就會被載入到記憶體中。
在這裡我們可以做個這樣的測試:

將上述代碼修改為:

public class Singleton {
private Singleton(){
System.out.println("createSingleton");
};
private static Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
public static void testSingleton(){
System.out.println("CreateString");
}
}

而我們在另外一個測試類別中對它進行測試(本例所有測試都通過Junit進行測試)

public class TestSingleton {
@Test
public void test(){
Singleton.testSingleton();
}
}

輸出結果:

createSingleton
CreateString

我們可以注意到,在這個單例中,即使我們沒有使用單例類,它還是被建立出來了,這當然是我們所不願意看到的,所以也就有了以下一種單例。

2.懶漢式單例

public class Singleton1 {
private Singleton1(){
System.out.println("createSingleton");
}
private static Singleton1 instance = null;
public static synchronized Singleton1 getInstance(){
return instance==null?new Singleton1():instance;
}
public static void testSingleton(){
System.out.println("CreateString");
}
}

上面的單例擷取執行個體時,是需要加上同步的,如果不加上同步,在多線程的環境中,當線程1完成建立單例操作,而在完成賦值操作之前,線程2就可能判
斷instance為空白,此時,線程2也......餘下全文>>
 
java 單例模式?怎寫

class AAA{
private static AAA a=null;
private AAA(){

}
public static AAA getInstract(){
if(==null){
a=new AAA();
}
return a;
]
}

關鍵就是私人的構造方法,再做一個能拿到對象的入口
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.