What Is a Singleton in C#

What is a singleton in C#?

A singleton is a class which only allows one instance of itself to be created - and gives simple, easy access to said instance. The singleton premise is a pattern across software development.

There is a C# implementation "Implementing the Singleton Pattern in C#" covering most of what you need to know - including some good advice regarding thread safety.

To be honest, It's very rare that you need to implement a singleton - in my opinion it should be one of those things you should be aware of, even if it's not used too often.

Why do we use Singleton?

Singleton Patern is used when we want to be sure, that instance of your class in created only one time.
Image a situation that You have such a two classes:

    public class MessageBuilder {

public MessageBuilder() { }

public string BuildMessage(string message) {
return message;
}
}

public class MessageDisplayer
{

public MessageDisplayer() { }

public void DisplayErrorMessage(string message)
{
MessageBuilder builder = new MessageBuilder();
Console.Write(builder.BuildMessage("Error message"));
}

public void DisplaySuccessMessage(string message)
{
MessageBuilder builder = new MessageBuilder();
Console.Write(builder.BuildMessage("Success message"));
}
}

One class contain some logic to build message and another to show it. But as You can see in methods of class MessageDisplayer we have to create an instance of MessageBuilder twice. Let's rewrite it due to Singleton Patern:

public class MessageBuilder
{
private static readonly MessageBuilder instance = new MessageBuilder();

private MessageBuilder() { }

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

public string BuildMessage(string message)
{
return message;
}
}

public class MessageDisplayer
{

public MessageDisplayer() { }

public void DisplayErrorMessage(string message)
{
MessageBuilder builder = MessageBuilder.Instance;
Console.Write(builder.BuildMessage("Error message"));
}

public void DisplaySuccessMessage(string message)
{
MessageBuilder builder = MessageBuilder.Instance;
Console.Write(builder.BuildMessage("Success message"));
}
}

Now you will create and instance of of MessageBuilder only once, and on next call of property Instance it would not create new instance, but just return you one is stored in private variable instance.

Difference between static class and singleton pattern?

What makes you say that either a singleton or a static method isn't thread-safe? Usually both should be implemented to be thread-safe.

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common, in my experience), so you can pass around the singleton as if it were "just another" implementation.

Singleton Pattern for C#

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.

What is the main difference between static class & Singleton class

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.

Use singleton when you need a class that has only one instance, and you need to provide a global point of access to the instance



Related Topics



Leave a reply



Submit