user65663
user65663

Reputation:

Replace singletons with what?

Singletons are so often said to be a bad design choice, so how should you design an application when you want to avoid them?

Upvotes: 9

Views: 1933

Answers (2)

Andrey Taptunov
Andrey Taptunov

Reputation: 9495

My 2 cents.

Just design your application in such way that it's realy doesn't matter if it's singleton passed to your object or not.

Consider if you have MySingleton.Instance inside -> that looks bad and you are tightly coupled with this. If you passed MySignleton as a parameter in a method and it's instantiated outside as a MySingleton.Instance or new MySingleton() -> oh well, I still can mock or change it so it doesn't really matter.

Upvotes: 2

Asaf David
Asaf David

Reputation: 3693

you can use concepts such Dependency Injection to inject the services you depend on (basically the singletons you use) to you, instead of providing a global reference to them

Upvotes: 8

Related Questions