views:

246

answers:

3

Hi guys,

I am wondering if it is possible to have a static class instantiate another class for the purpose of holding a reference to it globally. I have a data store which is composed of an in-memory object and would like to access it from different locations. The data needs to persist changes to the application so it needs to be instantiated outside of window or UI scope.

I was hoping that using a static class to do this would be the correct way of doing it. Is this what a singleton is? Is what I am looking for possible?

+9  A: 

The normal way of handling this is to use the Singleton Pattern. This basically creates a single instance of a non-static class, which can be accessed statically (typically by using the ClassName.Instance property).

Here's a great page on creating a singleton in C#.

Reed Copsey
Im in work, just leaving, I will take a look at your link when I get home, cheers for the input!
DeanMc
+1  A: 

Here is a simple example.... you could get the property just by using Config.Instance.Value

public class Config
{
    private Config() 
    {
        this.Value = "foobarr";
    }
    private static object _syncLock = new object();
    private static Config _instance;
    public static Config Instance
    {
        get
        {
            lock (_syncLock)
            {
                if (_instance == null)
                    _instance = new Config();
                return _instance;
            }
        }
    }

    public string Value { get; private set; }
}
Matthew Whited
Yes you would probably want to look at locks and mutexs as well.
Matthew Whited
This actually won't compile, either. Also, in general, it's a VERY BAD idea to make unsealed static instances (ie: the protected set should be private set). The link I posted has a much better, more robust (threadsafe) singleton implementation.
Reed Copsey
Is it ok to have the instance as something other that the same type as the static caller? Also is it correct to assume that the gotten instance is the same instance each time? EDIT: sorry just seen your comment Reed, will check that link shortly!
DeanMc
@DeanMc: It is okay (although unusually) to have the instance be a different type than the caller, but this typically will be a subclass of the containing singleton's type. In this case, you'll probably need a factory method to construct the instance, and have to handle any synchronization yourself.
Reed Copsey
guess it helps when I click build... oh well that's what i get for winging it And I didn't include the lock mainly to reduce space. Also depends on what and how you are using it for if it is really needed. And yes different types are fine. The class holding the instance could even be a static class that as several singletons under it.
Matthew Whited
And for the protected I am just used to typing {get; protected set;} for some properties. The exmaple has been fixed an now compiles :)
Matthew Whited
This, however, is NOT a threadsafe implementation of a singleton. Do not use this in multithreaded scenarios. Jon Skeet's implementation (which I linked to) has better performance, is thread safe, and is a simpler implmentation - I'd recommend using it.
Reed Copsey
There I added the lock. If you move this to a static constructor the lock is pretty much useless.
Matthew Whited
At least this is now a safe (if inefficient) implementation. Thanks for fixing it.
Reed Copsey
+1  A: 

A static class can create and reference any object just like any other class. It's not quite a singleton, but you get a similar end result. The static class is "constructed" the first time it is referenced, so if you need something to happen before the window or UI stuff happens you'll still need to take care of that.

Whether an actual singleton is better, I don't know. There are quite a few people out there that say singletons and static classes are bad, mostly because they make the code a little more rigid. It's almost a global variable in a way.

David Hogue
Singletons are bad in exactly the same way static classes (with data) are bad - they're merely criticized because it's static. If your going static anyways, at least use an established pattern (singleton).
Reed Copsey
That's true, post updated.
David Hogue