C
C#2y ago
surwren

Trying to wrap my head around singletons

I'm confused by the techopedia definition being a "global variable" whereas wikipedia states (contradictorily) that a singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance Just how many things can the word 'singleton' refer to in a programming context?
2 Replies
Thinker
Thinker2y ago
A singleton is an instance which is available everywhere, and the class can typically only have a single instance. Technically a singleton doesn't have to be globally available, though, it just has to be the only instance of a class.
Anton
Anton2y ago
It's a single instance, except when it's not, and it's global, except when it's not. For example: 1. global, single instance A class that just represents some behaviour. Say, you wanted to implement an interface with a method void A and you make that method just do nothing in the implementation. Then you could make the users of that class just access a single instance, created beforehand, rather than making them create a new one each time.
public interface IInterface
{
void A();
}
public class Impl : IInterface
{
// Only a single instance.
public static readonly Impl Instance = new();
// Prevent instantiating normally.
private Impl(){}
// The method implementation does nothing, for example.
public void A(){}
}
public interface IInterface
{
void A();
}
public class Impl : IInterface
{
// Only a single instance.
public static readonly Impl Instance = new();
// Prevent instantiating normally.
private Impl(){}
// The method implementation does nothing, for example.
public void A(){}
}
2. global, multiple instances A similar situation like above, but it has a boolean argument to the constructor. Then you could create two instances, one for true and one for false. It would still be a singleton.
public interface IInterface
{
void A();
}
public class Impl : IInterface
{
public static readonly Impl InstanceTrue = new(true);
public static readonly Impl InstanceFalse = new(false);
// Prevent instantiating normally.
private Impl(bool b){}
// The method implementation does nothing, for example.
public void A(){}
}
public interface IInterface
{
void A();
}
public class Impl : IInterface
{
public static readonly Impl InstanceTrue = new(true);
public static readonly Impl InstanceFalse = new(false);
// Prevent instantiating normally.
private Impl(bool b){}
// The method implementation does nothing, for example.
public void A(){}
}
3. not global, single/multiple instance In this case you would just create an instance of some class only in a single place in your program, and then pass it to any function that needs the implementation. Technically, in this case you can make any number of instances, and then pass different instances around. Or create separate entities for separate contexts. What I mean is that you technically could have multiple DI containers in a program, to each of which you'd bind a singleton implementation, so you could have multiple such instances in a program but it would still be a singleton. Some people do global stateful singletons, which is almost always bad for even somewhat complicated programs But doing global stateless behavior instances is only logical
Want results from more Discord servers?
Add your server
More Posts