Singletons do not violate SRP except when poorly designed. They are a perfect pattern when needed. The are used to obtain adressibility, to an object often with functions,methods or properties pertaining to the object name which describes the responsibility.
There are other patterns which work as well, in particular events and event handlers. If the event definition is static then the ability is similar to a singleton.
In Javascript, the function, a true first class citizen which diminishes the need for singletons.
Finally, observables which are extensions of the event pattern may be static too. Event bus design often use this pub/sub pattern also diminishing the need for the singleton
All work, all are good so design wisely and there won't be any issues.
A software engineer with 4+ years of experience who loves working with JavaScript and loves refactoring to the point that I look weekly into old codebase of mine and refactor some parts of it!
I totally agree with you, they could be perfect in such use cases but I generally would not recommend the using of such pattern. It just doesn't feel right to have something globally swimming in your codebase and you'd have to design it carefully to take care of your unit tests and to disallow other functions or classes to manipulate it without being authorized to.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Singletons do not violate SRP except when poorly designed. They are a perfect pattern when needed. The are used to obtain adressibility, to an object often with functions,methods or properties pertaining to the object name which describes the responsibility.
There are other patterns which work as well, in particular events and event handlers. If the event definition is static then the ability is similar to a singleton.
In Javascript, the function, a true first class citizen which diminishes the need for singletons.
Finally, observables which are extensions of the event pattern may be static too. Event bus design often use this pub/sub pattern also diminishing the need for the singleton
All work, all are good so design wisely and there won't be any issues.
I totally agree with you, they could be perfect in such use cases but I generally would not recommend the using of such pattern. It just doesn't feel right to have something globally swimming in your codebase and you'd have to design it carefully to take care of your unit tests and to disallow other functions or classes to manipulate it without being authorized to.