1. 私有静态变量(在第四步供外界使用),创建类的实例
2. 私有构造函数,确保外部无法直接实例化(确保是单个实例)
3. 确定供外界调用的代码资源
4. 公开静态属性,供外界使用(把第一步类的实例,开放出去)
5. 外界使用
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 C_之单例模式
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//1. 私有静态变量(在第四步供外界使用)
//2. 私有构造函数,确保外部无法直接实例化
//3. 确定供外界调用的代码资源
//4. 公开静态属性,供外界使用
//5. 外界使用
private void button1_Click(object sender, EventArgs e)
{
// 5.外界进行使用
Camera.Instance.Snap();
}
}
/// <summary>
/// Camera进行单例 全局唯一存在, 方便调用,节省资源
/// </summary>
public class Camera
{
//1. 私有静态变量(在第四步供外界使用)
private static Camera intance = new Camera();
//2. 构造器,私有化
private Camera()
{
}
//4. 公开静态属性,供外界使用
public static Camera Instance
{
get { return intance; }
}
//3. 确定供外界调用的代码资源
public string CameraId { get; set; }
public void Snap()
{
MessageBox.Show("相机拍照");
}
}
}
单例模式
1.将类生成在程序启动之前,消失在程序结束之后,全局存在
2.唯一
好处:
1.避免重复生成,节省资源
2.方便调用
在C#中设置单例类通常意味着你想要确保你的应用程序中只有一个实例存在。
你可以使用静态属性和静态构造函数来创建单例。
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 属性,你将获得相同的对象实例。
这种方式确保了内存中只有一个对象实例,从而节省了内存,特别是在多个地方需要使用同一个对象实例的时候。
实例
1. 私有静态变量(在第四步供外界使用)
2. 私有构造函数,确保外部无法直接实例化
3. 确定供外界调用的代码资源
4. 公开静态属性,供外界使用
5. 外界使用
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");
}
}
}