單例模式代碼

來源:互聯網
上載者:User
1.加鎖的單例模式
public class CommonService
    {
        private static CommonService instance;
        private static readonly object syncRoot = new object();
        private CommonService()
        {
        }
        public static CommonService GetInstance()
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new CommonService();
                    }
                }
            }
            return instance;
        }

    }

2 懶漢式 第一次調用時初始CommonService ,以後就不用再產生了。 

靜態初始化
不需要顯示的編寫安全執行緒代碼,即可解決多線程環境下單例模式不安全的問題
    public sealed class CommonService
    {
        private static readonly CommonService instance = new CommonService ();
        private CommonService () { }
        public static CommonService GetInstance()
        {
            return instance;
        }

    }

另附:

1. 單例的目的是什嗎?  
   
這個應該很明顯,保證一個類只有單一的執行個體,也就是說你無法通過New或CreateInstance來建立這個類的一個新執行個體。
2. 單例的好處在哪裡?
   
當一個對象在程式內部只能有一個執行個體的時候,它可以保證我們不會重複建立,而是始終指向同一個對象。
3. 怎麼用?
     單例模式的實現代碼如下:

namespace SinglePattern
{
     public class SingleClass
     {
         private static SingleClass instance;

         protected SingleClass(){}

         public static SingleClass GetInstance()
         {
             if(instance == null)
             {
                 instance = new SingleClass();
             }
             return instance;
         }

}
}

上面的代碼,可以說是一個標準的單例的代碼,但是上述代碼在多線程的時候有可能會產生多個執行個體,為了避免這個情況的發生,我們需要限制同一時間,只能有一個線程訪問。

利用lock可以實現我們的目的:

namespace SinglePattern
{
     public class SingleClass
     {
         // 靜態變數
         private static SingleClass instance;

         // "鎖"變數
         private static object lockObject = new objest();

         // 受保護的建構函式
         protected SingleClass(){}

         // 靜態擷取對象的方法
         public static SingleClass GetInstance()
         {
             lock (lockObject)
             {
                 if (instance == null)
                 {
                     instance = new SingleClass();
                 }
             }
             return instance;
         }

     }
}

另一個方法:

這個方法經過調整之後,也可以用於限制一個表單只能啟動一個執行個體。

using System.Threading;
namespace SinglePattern
{
     public class SingleClass
     {
         // 靜態變數
         private static SingleClass instance;

         // 受保護的建構函式
         protected SingleClass(){}

// 靜態擷取對象的方法
         public static SingleClass GetInstance()
         {
Mutex mutex = new Mutex();
             mutex.WaitOne();

             if (instance == null)
             {
                 instance = new SingleClass();
             }

             mutex.Close();

             return instance;
         }

     }
}

 

聯繫我們

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