Creational Patterns: Singleton Flashcards

1
Q

What is a singleton?

A

A class designed to only ever have one instance. The class itself is responsible for enforcing this design requirement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three most common use cases of the Singleton?

A
  1. Access to file system (shared instance)
  2. Access to shared network resource (shared instance)
  3. When one-time configuration is expensive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the structure of the Singleton?

A

A single class with a private instance and a public static method that provides the only way to reference that instance. It must also have a private constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What can be said about the number of instances of a class that implements the Singleton.

A

They have, at any time in the life of an application, either 0 or 1 instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What can be said about the parameters needed to instantiate a Singleton class?

A

Singleton classes are created without parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When does a Singleton instance get created?

A

By default, when something requests them (lazy instantiation). However, it is also possible to simply create the instance you need when the application starts and then use that instance for the life of the app.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What can be said about the constructor of the Singleton classes and what does this imply?

A

Singleton classes should have a single, private, parameterless constructor. Because of this, subclassing is not allowed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What can be said about inheriting a Singleton class?

A

Singleton classes should be marked as sealed. Therefore, they cannot be inherited.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What can be said about the referencing a Singleton class?

A

A private static field holds the only reference to the instance. A public static method provides access to this field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the biggest problem with the naive implementation of the Singleton?

A

It is not thread safe.

In multithreaded environments, multiple threads can invoke a call to the constructor by accessing the Instance property/method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the most basic and obvious way to achieve thread safety for the Singleton?

A

To use a private padlock ‘object’ field and a lock block around the logic in the Instance getter.

get
{
    lock (_padlock)
        return _instance ??= new();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the drawback of using a lock statement and an object as the lock?

A

It has negative performance impacts because every use of the Instance property will incur the overhead of the lock.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

If you were to use locks, what should you use as the lock object?

A

A dedicated private instance variable. Lock statements should be paired one to one with their lock variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a simple way to improve the performance of a basic thread-safe Singleton that uses a lock?

A

To perform a null check on the instance before the lock statement.

get
{
    if (_instance is null)
        lock (_padlock)
            _instance ??= new();
    return instance;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the downside of using a lock to achieve thread safety in a singleton?

A

The most basic approach where we simply lock and return a new instance or the existing instance calls the lock statement in every call to the Singleton. This is bad for performance.

The double-checking approach where we check whether the instance is null before the lock statement is complex, easy to get wrong and doesn’t necessarily work with the ECMA common language interface specification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s an alternative to locking for achieving thread-safety in Singleton?

A

The C# feature of static type construction.

17
Q

When and how ofter do C# static constructors run?

A

They are called when any static member of a type is reference and they run only once per appdomain.

18
Q

What’s a concern about C# static constructors in regards to compiler issues?

A

You must use an explicit static constructor to avoid issues with the C# compiler and beforefieldinit.

19
Q

What is beforefieldinit?

A

It’s a hint the compiler uses to let it know static initialisers can be called sooner, and this is the default if the type doesn’t have an explicit static constructor.

20
Q

How does having an explicit static constructor effect beforefieldinit?

A

Having an explicit static constructor avoids having beforefieldinit applied, which helps make our Singleton behaviour lazier.