类本身是一个单例,类的字段是其他类的对象引用Reference,类的属性包装该字段。当需要对象引用Reference时,使用该类的唯一实例构造。
public class ViewModelsHelper
{
private static readonly object _lock = new object();
private static ViewModelsHelper Instance;
public static ViewModelsHelper GetInstance()
{
if (Instance == null)
{
lock (_lock)
{
if (Instance == null)
{
Instance = new ViewModelsHelper();
}
}
}
return Instance;
}
private TestViewModel testviewmodel;
public TestViewModel testViewModel {
get {
if (testviewmodel == null) {
testviewmodel = new TestViewModel();
}
return testviewmodel;
}
}
}
保证了创建的对象是一个单例