C#的單例模式實(shí)現(xiàn)-創(chuàng)新互聯(lián)

    只能生成一個實(shí)例的類是實(shí)現(xiàn)了Singleton(單例)模式的類。以下為C#實(shí)現(xiàn)單例模式的方式。
    
方式一只使用于單線程環(huán)境
    // 把構(gòu)造函數(shù)設(shè)為私有函數(shù)以禁止他人創(chuàng)建實(shí)例
    // 定義一個靜態(tài)的實(shí)例在需要的時(shí)候創(chuàng)建該實(shí)例
    // 在Singlrton的靜態(tài)屬性Instance中只有在instance為null的時(shí)候才創(chuàng)建一個實(shí)例以避免
    // 重復(fù)創(chuàng)建
    // 把構(gòu)造函數(shù)定義為私有函數(shù)
    public sealed class Singleton1
    {
        public int a = 2;
        private Singleton1() { }        private static Singleton1 instance = null;        public static Singleton1 Instance        {            get            {                if (instance == null)                    instance = new Singleton1();                return instance;            }        }    }     方式二雖然在多線程環(huán)境中能工作但效率不高     
// 每次通過屬性Instance得到Singleton2的實(shí)例都會試圖加上一個同步鎖
    // 而加鎖是一個非常耗時(shí)的操作在沒有必要的時(shí)候應(yīng)該盡量避免
    public sealed class Singleton2
    {
        public int a = 2;
        private Singleton2(){}
        private static readonly object syncObj = new object();
        private static Singleton2 instance = null;
        public static Singleton2 Instance
        {
            get
            {
                lock (syncObj)
                {
                    if (instance == null)
                        instance = new Singleton2();
                }
                return instance;
            }
        }
    }
    可行的解法 加同步鎖前后兩次判斷實(shí)例是否已存在        
// 只有instance為null即沒有創(chuàng)建時(shí)需要加鎖操作。
    public sealed class Singleton3
    {
        private Singleton3() { }
        private static readonly Object syncObj = new Object();
        private static Singleton3 instance = null;
        public static Singleton3 Instance
        {
            get
            {
                if(instance == null)
                {
                    lock(syncObj)
                    {
                        if(instance == null)
                            instance = new Singleton3();
                    }
                }
                return instance;
            }
        }
    }
        推薦的解法一利用靜態(tài)構(gòu)造函數(shù)     
   // 在初始化靜態(tài)變量instance的時(shí)候創(chuàng)建一個實(shí)例
    // 由于C#是在調(diào)用靜態(tài)構(gòu)造函數(shù)時(shí)初始化靜態(tài)變量.NET運(yùn)行時(shí)能夠確保只調(diào)用一次靜態(tài)構(gòu)造
    // 函數(shù)保證只初始化一次instance
    public sealed class Singleton4
    {
        private Singleton4() { }
        private static Singleton4 instance = new Singleton4();
        public static Singleton4 Instance
        {
            get
            {
                return instance;
            }
        }
    }
        推薦的解法二 實(shí)現(xiàn)按需創(chuàng)建實(shí)例        
// 在內(nèi)部定義了一個私有類型Nested。
    // 當(dāng)?shù)谝淮斡玫竭@個嵌套類的時(shí)候會調(diào)用靜態(tài)構(gòu)造函數(shù)創(chuàng)建Singleton5的實(shí)例instance
    public sealed class Singleton5
    {
        private Singleton5() { }
        public static Singleton5 Instance
        {
            get
            {
                return Nested.instance;
            }
        }
        class Nested
        {
            static Nested() { }
            internal static readonly Singleton5 instance = new Singleton5();
        }
    }
        擴(kuò)展 定義一個表示總統(tǒng)的類型President可以從該類型繼承出FrenchPresident    和AmericanPresident等類型。這些派生類型都只能產(chǎn)生一個實(shí)例        
public class President
    {
        private string name = "";
        private string country = "";
        public President() { }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Country
        {
            get { return country; }
            set { country = value; }
        }
    }
        
public sealed class FrenchPresident: President
    {
        private FrenchPresident():base() { }
        private static FrenchPresident instance = new FrenchPresident();
        public static FrenchPresident Instance
        {
            get { return (FrenchPresident)(Nested.instance); }
        }
        private class Nested
        {
            static Nested() { }
            internal static readonly FrenchPresident instance = new FrenchPresident();
        }
    }
       
 public sealed class AmericanPresident : President
    {
        private AmericanPresident() : base() { }
        private static AmericanPresident instance = new AmericanPresident();
        public static AmericanPresident Instance
        {
            get { return Nested.instance; }
        }
        private class Nested
        {
            static Nested() { }
            internal static readonly AmericanPresident instance = new AmericanPresident();
        }
    }
        實(shí)現(xiàn)泛型單例模式        
public class SingletonExample<T> where T : class, new()
    {
        public static T Instance
        {
            get { return Nested.instance; }
        }
        private class Nested
        {
            static Nested() { }
            internal static readonly T instance = new T();
        }
    }
    public class Two: SingletonExample<Two>
    {
        public int a = 2;
        public void Show()
        {
            Console.WriteLine(a);
        }
    }

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

專業(yè)領(lǐng)域包括成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。

網(wǎng)站欄目:C#的單例模式實(shí)現(xiàn)-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article18/dpsegp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈品牌網(wǎng)站制作、App開發(fā)、全網(wǎng)營銷推廣網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司