Singleton模式 - 默认属性 [英] Singleton Pattern - Default Property

查看:337
本文介绍了Singleton模式 - 默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Singleton模式,因为它是在设置类中使用。下面是从Settings.Designer.cs的相关代码为我的项目AccessTest:

I've been studying the Singleton pattern as it is used in the Settings class. Here's the relevant code from Settings.Designer.cs for my project AccessTest:

internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{        
    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default
    {
        get
        {
            return defaultInstance;
        }
    }

    public string applicationSetting1
    {
        get
        {
            return ((string)(this["applicationSetting1"]));
        }
    }
}



什么我不清楚是

What is unclear to me is why the property 'applicationSetting1' is accessed through another property 'Default' like this:

var value = AccessTest.Properties.Settings.Default.applicationSetting1;



我运行VS2013 C#和4.5。

I'm running VS2013 C# and 4.5.

推荐答案

有单例模式的许多职位,但我找不到任何解决这一具体问题。所以,我想我会采取射击在使用这个例子,我在我的秘密实验室创建了你的帖子的帮助解释它自己:

There are a great many posts on the singleton pattern but I couldn't find any that address this particular issue. So I thought I'd take a shot at explaining it myself using this example that I created in my secret laboratory with the help of your posts:

namespace MyProject.Properties                                  
{                                   
    internal class Singleton                                
    {                               

        // Create an instance of the class itself.                          
        private static Singleton instance = new Singleton();                            

        // Wrap the instance in a public property.                          
        public static Singleton Instance                            
        {                           
            get {return instance;}                      
        }                           

        // Prevent additional references from being created with a private constructor.                         
        private Singleton() { }                         

        // Create a non-static variable.                                
        public string nonStatic = "non static";                         

    }                               
}                                   

我们知道下面这个辛格尔顿等级:

We know the following about this Singleton class:


  • 在实例公共财产只能静态访问

  • 非静态不能静态访问,也就是说,它只能是
    。通过参考访问。

  • 私人构造防止被创造了额外的
    引用。

那么,如何才可能从类外部访问非静态?

How, then, is it possible to access 'nonStatic' from outside the class?

该框架通过赋予具有神奇力量的,只有在一个单身的情况存在静态实例解决了难题:实例变成了桥梁,它可以访问任何非静。 ERGO,这种形式的作品:

The framework resolves the dilemma by endowing the static 'Instance' with magical powers that only exist in a singleton situation: 'Instance' becomes a "bridge" that provides access to any non-statics. Ergo, this form works:

var value = MyProject.Properties.Singleton.Instance.nonStatic;                                  

请注意:该模式用我的微软在Settings.Designer.cs文件似乎不是一个真正的单是因为默认的构造函数允许创建额外的引用。

Note: The pattern used my Microsoft in the Settings.Designer.cs file appears to not be a true singleton because the default constructor allows additional references to be created.

这篇关于Singleton模式 - 默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆