Singleton Pattern - Default Property

后端 未结 3 833
暗喜
暗喜 2021-01-21 01:46

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:

int         


        
3条回答
  •  不要未来只要你来
    2021-01-21 02:00

    Normaly, your Default property is called Instance

    So that you can call your singleton like this :

    Settings.Instance. X FUNCTION()
    

    Design Pattern by Erich Gamma is pretty solid on design pattern. You should be able to find a PDF on the web easily :)

    BTW you should also add this to your Default/Instance property

    If(defaultInstance == null)
    {
      defaultInstance = new Settings();
    }
    return defaultInstance
    

    That way your singleton is never null and is lazy instantiated

提交回复
热议问题