Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton Pattern for C# [closed]

Tags:

c#

singleton

I need to store a bunch of variables that need to be accessed globally and I'm wondering if a singleton pattern would be applicable. From the examples I've seen, a singleton pattern is just a static class that can't be inherited. But the examples I've seen are overly complex for my needs. What would be the very simplest singleton class? Couldn't I just make a static, sealed class with some variables inside?

like image 641
cam Avatar asked Apr 19 '10 11:04

cam


2 Answers

Typically a singleton isn't a static class - a singleton will give you a single instance of a class.

I don't know what examples you've seen, but usually the singleton pattern can be really simple in C#:

public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();
    static Singleton() {} // Make sure it's truly lazy
    private Singleton() {} // Prevent instantiation outside

    public static Singleton Instance { get { return instance; } }
}

That's not difficult.

The advantage of a singleton over static members is that the class can implement interfaces etc. Sometimes that's useful - but other times, static members would indeed do just as well. Additionally, it's usually easier to move from a singleton to a non-singleton later, e.g. passing in the singleton as a "configuration" object to dependency classes, rather than those dependency classes making direct static calls.

Personally I'd try to avoid using singletons where possible - they make testing harder, apart from anything else. They can occasionally be useful though.

like image 140
Jon Skeet Avatar answered Sep 29 '22 05:09

Jon Skeet


There are several Patterns which might be appropriate for you, a singleton is one of the worse.

Registry

struct Data {
  public String ProgramName;
  public String Parameters;
}

class FooRegistry {
  private static Dictionary<String, Data> registry = new Dictionary<String, Data>();
  public static void Register(String key, Data data) {
     FooRegistry.registry[key] = data;
  }
  public static void Get(String key) {
     // Omitted: Check if key exists
     return FooRegistry.registry[key];
  }
}

Advantages

  • Easy to switch to a Mock Object for automated testing
  • You can still store multiple instances but if necessary you have only one instance.

Disadvantages

  • Slightly slower than a Singleton or a global Variable

Static Class

class GlobalStuff {
  public static String ProgramName {get;set;}
  public static String Parameters {get;set;}
  private GlobalStuff() {}
}

Advantages

  • Simple
  • Fast

Disadvantages

  • Hard to switch dynamically to i.e. a Mock Object
  • Hard to switch to another object type if requirements change

Simple Singleton

class DataSingleton {
  private static DataSingleton instance = null;
  private DataSingleton() {}
  public static DataSingleton Instance {
     get {
         if (DataSingleton.instance == null) DataSingleton.instance = new DataSingleton();
         return DataSingleton;
     }
  }
}

Advantages

  • None really

Disadvantages

  • Hard to create a threadsafe singleton, the above Version will fail if multiple threads access the instance.
  • Hard to switch for a mock object

Personally I like the Registry Pattern but YMMV.

You should take a look at Dependency Injection as it's usually considered the best practice but it's too big a topic to explain here:

Dependency Injection

like image 39
Morfildur Avatar answered Sep 29 '22 05:09

Morfildur