Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does a singleton with only one public method make sense?

I have a class that exposes only one public method and it’s a singleton.

Would it make sense to just get rid of the class thing and declare properties and methods as global variables and functions? And then exporting only the one "public" function.

Is there anything wrong about using global variables?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In general, avoid the singleton pattern altogether if your class has state, since that becomes global state. But if you can’t (easily):

Can I get rid of the class thing and declare properties and methods as variables and functions? And then exporting only the one "public" function?

Yes, this makes total sense with modules. A module is essentially a singleton already (it gets evaluated only once). Notice the variables and functions wouldn’t be global, they’re scoped to the module.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading