anotherOne
anotherOne

Reputation: 1573

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?

Upvotes: 0

Views: 37

Answers (1)

Bergi
Bergi

Reputation: 664599

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.

Upvotes: 1

Related Questions