PositiveGuy
PositiveGuy

Reputation: 47733

Singleton Properties

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?

Upvotes: 17

Views: 34473

Answers (7)

AliSalehi
AliSalehi

Reputation: 355

in newer versions of C# you can write it like this:

private static MyObject _instance;
public static MyObject Instance
{
    get => _instance ??= new();
}

basically you can assign a value to _instance if its null and you will return the _instance either it had already a value or it just been assigned a new one. much more cleaner

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245389

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!";

Upvotes: 28

Luhmann
Luhmann

Reputation: 3890

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.

Upvotes: 1

Daniel A.A. Pelsmaeker
Daniel A.A. Pelsmaeker

Reputation: 50276

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

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292345

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.

Upvotes: 3

Andrew
Andrew

Reputation: 12009

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.

Upvotes: 2

hunter
hunter

Reputation: 63502

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

Upvotes: 3

Related Questions