二十三.單例模式

來源:互聯網
上載者:User

單例模式:

       保證一個類僅有一個執行個體,並提供一個訪問它的全域訪問點。

       類自身負責儲存它的唯一執行個體。

注意:多線程

       使用鎖,或C#靜態類——Demo3

 

 

Demo1:——WinForm

       public partial class Form1 : Form

    {

       public Form1()

       {

           InitializeComponent();

       }

 

       private void Form1_Load(object sender, EventArgs e)

       {

           this.IsMdiContainer = true;

       }

 

       private void button1_Click(object sender, EventArgs e)

       {

           Form2.Show1().Show();

       }

    }

 

       public partial class Form2 : Form

    {

       private static Form2 f;

       private Form2()

       {

           InitializeComponent();

       }

       publicstatic Form2 Show1()

       {

           if (f == null || f.IsDisposed)

           {

                f = new Form2();

                f.MdiParent = Form1.ActiveForm;

           }

           return f;

       }

 

       private void Form2_Load(object sender, EventArgs e)

       {

 

       }

    }

 

Demo2:

       classProgram

    {

       static voidMain(string[] args)

       {

           child c1= child.GetChild();

           child c2 = child.GetChild();

           c1.A = 5;

           Console.WriteLine(c2.A);

           Console.ReadKey();

       }

    }

   class child

    {

       private static child c;

       private child() { }

        public staticchild GetChild()

       {

           if (c == null)

           {

                c = new child();

           }

           return c;

       }

       private int a;

       public int A

       {

           get { return a; }

            set { a = value; }

       }

    }

 

Demo3:

1:

              privatestatic child c;

       private static readonly object syncRoot = newobject();//鎖

       private child() { }

       public static child GetChild()

       {

           lock (syncRoot)

           {

                if (c == null)

               {

                    c = new child();

                }

           }

           return c;

       }

 

2:雙重鎖

       privatestatic child c;

       private static readonly object syncRoot = new object();//鎖

       private child() { }

       public static child GetChild()

       {

           if (c == null)

            {

                lock (syncRoot)

                {

                    if (c == null)

                    {

                        c = new child();

                    }

                }

            }

           return c;

       }

 

3:靜態類

              sealed class child//sealed聲明靜態類,阻止派生增加執行個體

    {

       privatestatic readonly child c=new child();//第一次引用類的任何成員時建立執行個體。公用語言運行庫負責處理變數初始化

       private child() { }

       public static child GetChild()

       {

           returnc;

       }

       private int a;

       public int A

       {

           get { return a; }

           set { a = value; }

       }

    }

 

聯繫我們

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