单例模式-C#实现

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

一 Model

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

namespace 设计模式练习.Model.单例模式
{

    //单例模式的实现
    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;
        }


        private string name;
        private int age;
        private string sex;

        public string Name { get => name; set => name = value; }
        public int Age { get => age; set => age = value; }
        public string Sex { get => sex; set => sex = value; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.单例模式
{
    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;
        }
    }
}

二 View

<Window x:Class="设计模式练习.View.单例模式.Singleton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.单例模式"
        mc:Ignorable="d"
        Title="Singleton" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Label Content="boss姓名:" Grid.Row="0" Grid.Column="0"/>
        <Label Content="boss年龄:" Grid.Row="1" Grid.Column="0"/>
        <Label Content="boss性别:" Grid.Row="2" Grid.Column="0"/>

        <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
        <TextBlock Text="{Binding Age}" Grid.Row="1" Grid.Column="1"/>
        <TextBlock Text="{Binding Sex}" Grid.Row="2" Grid.Column="1"/>

        <Button Content="李总" Command="{Binding LiCommand}" Grid.Row="0" Grid.Column="2"/>
        <Button Content="谢总" Command="{Binding XieCommand}" Grid.Row="1" Grid.Column="2"/>
        <Button Content="张总" Command="{Binding ZhangCommand}" Grid.Row="2" Grid.Column="2"/>

        <Label Content="{Binding Boss1}" Grid.Row="0" Grid.Column="3"/>
        <Label Content="{Binding Boss2}" Grid.Row="1" Grid.Column="3"/>
        <Label Content="{Binding Boss3}" Grid.Row="2" Grid.Column="3"/>

        <Button Content="启动线程1" Command="{Binding t1Command}" Grid.Row="0" Grid.Column="4"/>
        <Button Content="启动线程2" Command="{Binding t2Command}" Grid.Row="1" Grid.Column="4"/>
        <Button Content="启动线程3" Command="{Binding t3Command}" Grid.Row="2" Grid.Column="4"/>
    </Grid>
</Window>



三 ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.单例模式;

namespace 设计模式练习.ViewModel.单例模式
{
    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);
                }

            });
        }
    }
}

相关推荐

  1. C++ 实现模式

    2024-01-27 01:52:01       31 阅读
  2. C++实现模式

    2024-01-27 01:52:01       13 阅读
  3. C#模式的简单实现

    2024-01-27 01:52:01       27 阅读
  4. C++模式实现

    2024-01-27 01:52:01       30 阅读
  5. C++实现模式

    2024-01-27 01:52:01       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 01:52:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 01:52:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 01:52:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 01:52:01       20 阅读

热门阅读

  1. WordPress wp-file-manager 文件上传漏洞 CVE-2020-25213

    2024-01-27 01:52:01       30 阅读
  2. element中form校验中清除校验不通过的提示语

    2024-01-27 01:52:01       36 阅读
  3. 【Git】Conventional Commit提交规范

    2024-01-27 01:52:01       29 阅读
  4. [GN] Vue3.2 快速上手 ----常用API及其新组件

    2024-01-27 01:52:01       32 阅读
  5. CentOS7开机自动执行脚本

    2024-01-27 01:52:01       38 阅读
  6. 算法37:最大矩形(力扣84、85题)---单调栈

    2024-01-27 01:52:01       37 阅读
  7. KMean 聚类

    2024-01-27 01:52:01       36 阅读
  8. LED闪烁

    2024-01-27 01:52:01       30 阅读
  9. live555搭建流式rtsp服务器

    2024-01-27 01:52:01       39 阅读
  10. openssl3.2/test/certs - 075 - non-critical unknown extension

    2024-01-27 01:52:01       27 阅读