14

Ok, if I create a singleton class and expose the singleton object through a public static property...I understand that.

But my singleton class has other properties in it. Should those be static? Should those also be private?

I just want to be able to access all properties of my singleton class by doing this:

MySingletonClass.SingletonProperty.SomeProperty2

Where SingletonProperty returns me the single singleton instance. I guess my question is, how do you expose the other properties in the singleton class..make them private and then access them through your public singleton static property?

Or should all your other properties and methods of a singleton be public non-static?

M.A. Hanin
  • 8,044
  • 33
  • 51
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471

6 Answers6

27

Once you get the SingletonProperty (which is the single instance of an Object), anything after that can be implemented as if you were creating a class to be instanciated because the Singleton is really a single instance of a regular Object.

For example, the following Singleton (obviously not the best Singleton design, but bear with me) offers up two public Properties called Value and Name:

public class MySingleton
{
    private static MySingleton _instance;    

    private MySingleton() { }

    public static MySingleton Instance
    {
        get
        {
            if(_instance == null)
                _instance = new MySingleton();

            return _instance;
        }
    }

    // Your properties can then be whatever you want
    public string Value { get; set; }

    public string Name { get; set; }
}

Accessing these properties would look like:

MySingleton.Instance.Name = "StackOverflow";

MySingleton.Instance.Value = "Rocks!";
Tim
  • 7,746
  • 3
  • 49
  • 83
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • gotcha. While the rest are all public the only way to get at them is through whatever property exposes the singleton instance. Thanks. – PositiveGuy Mar 08 '10 at 21:22
  • 6
    Reading and writing from/to the Name and Value properties are not thread-safe; you'd need to utilize a lock object on each property unfortunately. – The Light Jul 09 '11 at 22:09
  • i dont know how this was selected correct answer? or perhaps, Skeet didnt see it. How can you access `MySingleton.Instance` if it s not static? – DarthVader Jan 20 '12 at 09:08
  • @DarthVader - Good catch...even after such a long time. – Justin Niessner Jan 20 '12 at 14:30
  • singleton changed after all that time i guess:) – DarthVader Jan 20 '12 at 19:49
  • What about double check ? http://stackoverflow.com/questions/5950418/double-check-locking-in-singleton-pattern http://csharpindepth.com/Articles/General/Singleton.aspx – Kiquenet Mar 12 '13 at 10:24
3

Make them public properties as you would any other class. Using the singleton pattern would be independent of this.

hunter
  • 62,308
  • 19
  • 113
  • 113
2

So long as they're not static, you need an object instance to access the property. And if the only way to create the object instance is via the singleton pattern, your class properties are inherently part of the single class instance. Nothing special is required.

Andrew
  • 11,894
  • 12
  • 69
  • 85
2

No, let them be public. Since there can be only one instance of the class, the only way to access those properties will be through the single instance.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

They should be non-static public properties.

Think of it like this. You one one single instance of this class - but still an instance.

So you make your constructor private, and create a static property that handles the lazy instantiation.

All the other properties, members and methods should just be part of the instance - an instance that you now have ensured, will be the one and only.

Luhmann
  • 3,860
  • 26
  • 33
0

Your MySingletonClass.SingletonProperty returns a reference to an instance of the Singleton class. Therefore, you may use public properties (and methods and such) just like with any other instance of a class.

MySingletonClass.SingletonProperty.SomeProperty2

When you would make SomeProperty2 static, you could do the following (but this is not in the spirit of the Singleton design pattern):

MySingletonClass.SomeProperty2
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157