1

Many articles in Internet say that singleton is an anti-pattern, because it makes debugging more difficult.

However I don't understand why debugging a program with a singleton object is difficult. Please explain.

I think that singletons are inevitable, when we need (usually for performance reasons) lazy initialization of a controller object. Or are there any other alternatives?

porton
  • 791

1 Answers1

-1

Singletons are not always inevitable, you can use the monostate pattern in a lot of cases, however, it has the same issues as singletons, most notably it is to do with governing global state.

For why having global state is bad, take a look at the following stackoverflow post: Why is Global State so Evil?

Personally, I prefer to have a monostate class over a singleton as it allows me to inject this as a dependency rather than referencing the singleton's concrete implementation (via MyClass.Current), at least with a monostate you can abstract away the fact that there is a single instance of data, and allows me to test classes relying on it easier.

Matthew
  • 2,026