码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2015-05-27 21:01:35      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

============1单例模式==============

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

 

namespace ConsoleApplication13

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 0; i < 1000; i++)

            {

                Thread t = new Thread(new ThreadStart(() =>

                {

                    Singleton s = Singleton.GetInstance();

                }));

                t.Start();

            }

            Console.WriteLine("ok");

            Console.ReadKey();

 

        }

    }

 

    public class Singleton

    {

        private Singleton()

        {

            //只要输出一次.就证明创建了一个对象。

            Console.WriteLine(" . ");

        }

        private static Singleton _instance;

        private static readonly object _syn = new object();

        public static Singleton GetInstance()

        {

            //每次都锁定降低性能,所以只有为空值时才锁定。

            if (_instance == null)

            {

                lock (_syn)

                {

                    //为了防止在第一次判断_instance==null后,在锁定之前_instance又被赋值,所以锁定之后一定要再判断一次是否已经创建了对象。

                    if (_instance == null)

                    {

                        _instance = new Singleton();

                    }

                }

 

            }

            return _instance;

 

        }

    }

}

=============================2单例模式(另一种写法)==========================

 public sealed class Singleton2

    {

        private Singleton2()

        {

            Console.WriteLine(".");

        }

        //静态成员初始化,只在第一次使用的时候初始化一次。

        private static readonly Singleton2 _instance = new Singleton2();

 

        public static Singleton2 GetInstance()

        {

            return _instance;

        }

    }

=================测试====================

  for (int i = 0; i < 1000; i++)

            {

                Thread t = new Thread(new ThreadStart(() =>

                {

                    Singleton2 s = Singleton2.GetInstance();

                }));

                t.Start();

            }

            Console.WriteLine("ok");

            Console.ReadKey();

//======================实现窗口类(Form)的单例模式===============

public partial class Form2 : Form

    {

        private Form2()

        {

            InitializeComponent();

        }

        private static Form2 f2;

        private static readonly object syn = new object();

        public static Form2 GetForm2()

        {

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

            {

                lock (syn)

                {

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

                    {

                        f2 = new Form2();

                    }

                }

               

            }

            return f2;

        }

    }

单例模式

标签:

原文地址:http://www.cnblogs.com/luwei-s/p/4534342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!