Honestly, Singleton is that one pattern that doesn't make much sense to use in a language like Javascript where you can create one-off object instances without a class and define functionality for it.
The Singleton's getInstance pattern is more useful in languages like Java where managing when an instance is created and used is a bit more painful, but in js you can easily make sure a single instance is created by declaring it on the package scope, any further imports do not re-run the file, and it just uses the same instance.
I agree. Singletons are less of a design pattern and more of a work-around for the limitation of languages that only let you use classes and nothing else.
And apart from some sense of "this must be readable because I'm using design patterns" I think most people will find
// Singleton.jsexportconstSingleton={name:"John"};// some other fileimport{Singleton}from"...";
way more readable than the 2 different files presented in the example, with so much ceremony around what you are actually trying to accomplish.
A skilled software engineer, talented problem-solver and team player with a passion for using technology to solve complex problems and constantly seeking out new challenges to advance my skills👨🏼💻
Hey Mahamed Belkheir, you are right we can create one-off object instance directly. I prefer to follow the common implementation way of these design patterns. It also helps to understand the code better if someone is coming from other language. At last, in javascript we have two choices, modularisation approach or singleton approach.
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.
Honestly, Singleton is that one pattern that doesn't make much sense to use in a language like Javascript where you can create one-off object instances without a class and define functionality for it.
The Singleton's getInstance pattern is more useful in languages like Java where managing when an instance is created and used is a bit more painful, but in js you can easily make sure a single instance is created by declaring it on the package scope, any further imports do not re-run the file, and it just uses the same instance.
I agree. Singletons are less of a design pattern and more of a work-around for the limitation of languages that only let you use classes and nothing else.
And apart from some sense of "this must be readable because I'm using design patterns" I think most people will find
way more readable than the 2 different files presented in the example, with so much ceremony around what you are actually trying to accomplish.
Hey Mahamed Belkheir, you are right we can create one-off object instance directly. I prefer to follow the common implementation way of these design patterns. It also helps to understand the code better if someone is coming from other language. At last, in javascript we have two choices, modularisation approach or singleton approach.