设计模式- 单例模式

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;
            }
        }
    }
}

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

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窗体

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值