C# 設計模式之單件模式

來源:互聯網
上載者:User

     單件模式要求一個類有且僅有一個執行個體,同時提供一個全域的訪問點。一般的解決方案是對一個單件對象進行延遲初始化,即當第一次使用這個對象的時候才對這個對象進行初始化!

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SinglePattern
...{
    /**//*
    //---------------靜態初始化 不可取,但是可以作為對比來瞭解-------------------------------
    /// <summary>
    /// 第一次引用類的任何成員時建立執行個體
    /// 靜態建構函式和執行個體建構函式之間的區別,因為靜態建構函式是由CLR調用執行的,所以靜態建構函式只能是一個,同時不能還有參數。
    /// 第一, 代碼的執行順序,代碼在前的先執行;
    /// 第二, 靜態成員初始化語句要先於靜態建構函式執行;
    /// 第三,靜態成員初始化語句與靜態建構函式只執行一次。
    /// 什麼方法來初始化靜態成員?
    /// 第一, 簡單靜態成員,例如類型為實值型別等,使用成員初始化語句來完成;
    /// 第二, 靜態成員初始化比較複雜,或者有可能出現異常,那麼用靜態建構函式來完成。
    /// </summary>
    public sealed class Singleton
    {
        private static readonly Singleton instance;

        static Singleton()
        {
            instance = new Singleton();
        }

        /// <summary>
        /// 私人的建構函式
        /// </summary>
        Singleton()
        { 
        }

        /// <summary>
        /// 全域訪問點
        /// </summary>
        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }
    }
    //--------------------------------------------------------------
     */

    //---------------延遲初始化-------------------------------------
    public sealed class Singleton
    ...{
        Singleton()
        ...{
        }

        public static Singleton Instance
        ...{
            get
            ...{
                return Nested.instance;
            }
        }

        class Nested
        ...{
            internal static readonly Singleton instance;

            static Nested()
            ...{
                instance = new Singleton();
            }            
        }

        private int num = 0;

        /**//// <summary>
        /// 增加 
        /// </summary>
        public void Add()
        ...{
            num++;
            Console.WriteLine("輸出:" + num);
        }

    }

    //--------------------------------------------------------------

    class Program
    ...{
        public static void Print()
        ...{
            Singleton singleton = Singleton.Instance;
            lock (singleton)
            ...{
                for (int i = 0; i < 5; i++)
                ...{
                    singleton.Add();
                }
            }
        }

        static void Main(string[] args)
        ...{
            Thread threadFst = new Thread(new ThreadStart(Print));
            threadFst.Name = "Fst";
            Thread threadScd = new Thread(new ThreadStart(Print));
            threadScd.Name = "Scd";
            threadFst.Start();
            threadScd.Start();
            Console.ReadLine();
        }
    }
}

聯繫我們

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