设计模式 单例模式

Gof 定义

保证一个类仅有一个实例,并提供一个该实例的全局访问点。

理解

单例设计模式,我看到好像网上也有叫他作单件设计模式。其实故名思义,就是只能产生一个对象。在我们的生活中单例设计模式是很常见的,例如地球是唯一的。好像我前不久看的一部连续剧爱情公寓里说一颗宝石--沙漠之星,那个也是唯一的。还有你自己本身也是唯一,地球上没有一个和你完全一样的人。

最简单的单例设计模式CODE

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Singleton singleton = new Singleton();//不可行 单例设计模式 不能new出多个对象来。
            Singleton singleton = Singleton.Instance;

            Console.ReadLine();
        }
    }

    public class Singleton
    {
        private static Singleton _instance;

        private Singleton() { }

        public static Singleton Instance
        {
            get 
            {
                if (_instance == null)
                    _instance = new Singleton();
                return _instance;
            }
        }
    }
}

多线程的单例设计模式

  在多个线程访问单例设计模式的时候,由于线程之间的切换,可能会产生多个单例。这违返了单例设计模式的原则。使用lock确保一个线程位于代码的临界区,另一个线程不进入临界区,可以解决多线程产生多个单例的情况

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Singleton singleton = new Singleton();//不可行 单例设计模式 不能new出多个对象来。
            Singleton singleton = Singleton.Instance;

            Console.ReadLine();
        }
    }

    public class Singleton
    {
        private static Singleton _instance;
        private static readonly Object sync = new object();
        private Singleton() { }

        public static Singleton Instance
        {
            get 
            {
                lock (sync)//上锁
                {
                    if (_instance == null)
                        _instance = new Singleton();
                }
                return _instance;
            }
        }
    }
}

进一步写法 多线程的单例设计模式

View Code
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Singleton singleton = new Singleton();//不可行 单例设计模式 不能new出多个对象来。
            Singleton singleton = Singleton.Instance;

            Console.ReadLine();
        }
    }

    public class Singleton
    {
        private static Singleton _instance;
        private static readonly Object sync = new object();
        private Singleton() { }

        public static Singleton Instance
        {
            get 
            {
                if (_instance == null)
                {
                    lock (sync)//上锁
                    {
                        if (_instance == null)
                            _instance = new Singleton();
                    }
                }
                return _instance;
            }
        }
    }

}

单例设计模式的简单应用,只能新建一个FORM窗体

新建一个FORM1和FORM2, 将FORM1的isMidContainer属性设为TRUE;对Form2使用单例设计模式 CODE:

        public static Form2 _instance = null;
        public static Form2 Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Form2();
                }
                return _instance;
            }
        }

        private Form2()
        {
            InitializeComponent();
        }

在FORM1里拉一个MENU菜单,在菜单项里面的一个项中建一个CLICK方法。方法的 CODE

       private void 建一个新窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 fm = Form2.Instance;
            fm.MdiParent = this;
            fm.Show();
        }

这时候只能产生一个单例了,无论我点新建多少下,只能产生一个FORM2窗体

原文地址:https://www.cnblogs.com/cxeye/p/2695033.html