C#演示单例模式

单例模式的特点:
确保一个类只有一个实例,并提供一个全局访问点。

 //单例模式的实现
    internal class Boss
    {
        //定义静态变量保存实例
        private static Boss uniqueBoss;

        //定义锁,确保线程访问安全
        private static readonly object _lock = new object();

        //定义私有构造函数,使外界不能创建该类实例
        private Boss()
        {

        }

        //定义公有方法提供一个全局访问点,
        public static Boss GetBoss()
        {
            //关键代码加锁
            lock (_lock)
            {
                //如果类的实例不存在则创建,否则直接返回
                if (uniqueBoss == null)
                {
                    uniqueBoss = new Boss();
                }
            }

            return uniqueBoss;
        }


    }


    //双判断模式的写法:
     internal class Person
    {
        private Person() { }

        private static Person _instance;

        private static readonly object _instanceLock = new object();

        public static Person Instance()
        {
            if (_instance == null)
            {
                lock (_instanceLock)
                {
                    if (_instance == null)
                    {
                        _instance = new Person();
                    }
                }
            }

            return _instance;
        }
    }

实际使用:

 partial class Singleton_ViewModel : ObservableObject
    {

        //创建很多个Boss变量
        Boss boss_1 = Boss.GetBoss();
        Boss boss_2 = Boss.GetBoss();
        Boss boss_3 = Boss.GetBoss();

        [ObservableProperty]
        private string name;

        [ObservableProperty]
        private int age;

        [ObservableProperty]
        private string sex;

        [RelayCommand]
        private void Li()
        {
            Name = boss_1.Name = "李总";
            Age = boss_1.Age = 38;
            Sex = boss_1.Sex = "男";

            Boss1 = boss_1 == boss_2 ? "boss_1 = boss_2" : "boss_1 != boss_2";
        }

        [RelayCommand]
        private void Xie()
        {
            Name = boss_2.Name = "谢总";
            Age = boss_2.Age = 56;
            Sex = boss_2.Sex = "女";

            Boss2 = boss_1 == boss_2 ? "boss_1 = boss_2" : "boss_1 != boss_2";
        }

        [RelayCommand]
        private void Zhang()
        {
            Name = boss_3.Name = "张总";
            Age = boss_3.Age = 29;
            Sex = boss_3.Sex = "男";

            Boss3 = boss_2 == boss_3 ? "boss_2 = boss_3" : "boss_2 != boss_3";
        }

        [ObservableProperty]
        private string boss1;

        [ObservableProperty]
        private string boss2;

        [ObservableProperty]
        private string boss3;


        [RelayCommand]
        private void t1()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Li();
                    Task.Delay(1000);
                }

            });
        }

        [RelayCommand]
        private void t2()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Xie();
                    Task.Delay(1500);
                }

            });
        }

        [RelayCommand]
        private void t3()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Zhang();
                    Task.Delay(2000);
                }

            });
        }
    }

你可能感兴趣的:(C#,1024程序员节,c#,wpf,单例模式)