WPF 设置单例类

WPF 设置单例类

在WPF中设置单例类通常意味着你想要确保你的应用程序中只有一个实例存在。你可以使用静态属性和静态构造函数来创建单例。

public class SingletonExample : INotifyPropertyChanged
{
    // 私有静态变量
    private static SingletonExample instance = null;
 
    // 私有构造函数,确保外部无法直接实例化
    private SingletonExample()
    {
    }
 
    // 公有静态属性,用于获取单例实例
    public static SingletonExample Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new SingletonExample();
            }
            return instance;
        }
    }
 
    // 实现INotifyPropertyChanged接口的属性变更通知
    public event PropertyChangedEventHandler PropertyChanged;
 
    // 示例属性
    private string _exampleProperty;
    public string ExampleProperty
    {
        get { return _exampleProperty; }
        set
        {
            _exampleProperty = value;
            OnPropertyChanged("ExampleProperty");
        }
    }
 
    // 触发属性变更事件的方法
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

在上面的代码中,

SingletonExample 类通过私有构造函数和静态属性

Instance 实现了单例模式。

这意味着在整个应用程序中,无论你访问多少次 Instance 属性,你将获得相同的对象实例。

这种方式确保了内存中只有一个对象实例,从而节省了内存,特别是在多个地方需要使用同一个对象实例的时候。


实例

在这里插入图片描述

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 单例模式
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            // 第一种单例模式
            MessageBox.Show(Class1.Instance1.Name);
            Class1.Instance1.Show();

            // 第二种单例模式
            MessageBox.Show(Class2.Instance2.Name);
            Class2.Instance2.Show();
        }
    }
}

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

namespace 单例模式
{
    public class Class1
    {

        private static Class1 instance = new Class1();

        public static Class1 Instance1
        { get {  return instance; } }


        public string Name { get; set; }
        private Class1() {
            Name = "major1";
        
        }

        public void Show()
        {
            MessageBox.Show("Instance1");
        }

    }
}

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

namespace 单例模式
{
    public class Class2
    {
        private static Class2 class2 = null;

        private static readonly object lockObject = new object();

        public static Class2 Instance2
        {
            get
            {
                if(class2 ==null)
                {
                    lock(lockObject)
                    {
                        if(class2==null)
                        {
                            class2 = new Class2();
                        }
                    }
                }
                return class2;
            }
        }
        public  string Name { get; set; }
        public Class2()
        {
            Name = "major2";
        
        }

        public void Show()
        {
            MessageBox.Show("test2");
        }

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值