0

Consider a class that contains a property Name inside. I would like the class to be implemented as singleton, and I want the Name to be set only once (so it can be assigned once but not changed later).

Should I assign it with a constructor (and give it only get accessor, without set) or create a separate "instance variable" for "Name" and a proper method that will work only once?

The first option would force me to pass a string argument to the GetInstance() method every time I call it, while the second one does not seem too elegant for me (as I wouldn't know if "Name" was already set - so I'd need to call this method every time I try to get an instance anyway.) Am I taking a wrong approach? Is there a good practice for such case?

2
  • you can use the null coalescing operator (??) to lazily initialize it Jul 29, 2014 at 16:30
  • Thanks for your suggestion, but I'd still would not know when the name should be set - it would still force me to try to set the name every time I want to get an instance.
    – 3yakuya
    Jul 29, 2014 at 16:53

1 Answer 1

1

The problem with passing the value into getInstance() every time is that all calling classes will have to know where the value for Name comes from and how to fetch it. Maybe they do all have this access, but then it makes it redundant storing this data on the singleton object as all callers already know its value.

Assuming that some callers know the value, and others don't, you could use a property similar to the way that you have suggested yourself:

public class MySingleton
{
    // Singleton properties omitted

    private string name;

    public string name
    {
        get{return this.name;}
        set
        {
            if(String.IsNullOrEmpty(this.name))
                name = value;
            // The exception could be left out, depending on how critical this is
            else
                throw new exception("The property 'name' can only be set once");
        }
    }
}

This assumes that neither null or String.Empty are valid assignations for your property and its still not the most elegant solution, so perhaps there is a different approach altogether.

Perhaps the constructor for the singleton could fetch the required value, rather than being passed the value:

public MySingleton()
{
     name = configuration.getName(); // or wherever it is coming from
}

This way the calling classes can always assume that the singleton has a valid value for Name, but without caring where it comes from. If possible, I think this would be my preferred choice

1
  • This seems to be quite a good solution in this bad architecture (I shall try and rethink it all). Anyway, the exception should be caught somewhere low or not even thrown (as every GetInstance() would cause it to be thrown). Thanks for your time
    – 3yakuya
    Jul 31, 2014 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.