mercredi 24 février 2016

Why are singletons worse than having many references to the same object?

In this link: http://ift.tt/Xwl1Wz it says:

Singletons frequently are used to provide a global access point for some service. True, they do this, but at what cost? They provide a well-known point of access to some service in your application so that you don't have to pass around a reference to that service. How is that different from a global variable? (remember, globals are bad, right???) What ends up happening is that the dependencies in your design are hidden inside the code, and not visible by examining the interfaces of your classes and methods. You have to inspect the code to understand exactly what other objects your class uses. This is less clear than it could be.

I'm trying to understand what the difference is between this and having a reference to our object in many locations. If we have a reference held somewhere else, then couldn't our object be modified in some totally different location, say by some other thread, and be just as hard to reason about?

Aucun commentaire:

Enregistrer un commentaire