I've started programming after a long break. Been following a tutorial and came across Singleton Design Pattern. I want to know in what kind of situation the singleton pattern is used. Especially in game development.
-
Singletons are imo a bit of a horrible pattern. They can be somewhat useful for manager/system classes but even then you probably wouldn't want to write a traditional singleton as you'd likely want a system/manger to be bound to some instance such as a level. – George Jan 21 '18 at 17:45
4 Answers
The Singleton design pattern addresses all of these concerns. With the Singleton design pattern you can:
- Ensure that only one instance of a class is created
- Provide a global point of access to the object
- Allow multiple instances in the future without affecting a singleton class's clients

- 5,945
- 8
- 32
- 42

- 41
- 1
Singleton pattern is perhaps the most well known and abused pattern proposed by the Gang of Four very long time ago.
Many developers now consider singletons as a sort of anti-pattern. The most significant problem brought by sinetons might be that allows global access for some classes that shouldn't allow it. In short, it could violate encapsulation. Meanwhile, not because this pattern was bad but its misuses were.
You are strongly encouraged to take a look at Game Programming Patterns for more information. It is an excellent book for any programmers of any level.
Note that Service Locator pattern as one of the alternative suggested by the author, is debatable to be an anti-pattern. I'm still doubt with how to avoid singletons, but I haven't come up with a solution yet.

- 162
- 1
- 6
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
.

- 790
- 7
- 20