Asked • 07/29/19

Purpose of singletons in programming?

This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever created.This sounds a lot like a static class to me. The main difference being that with a static class you don't / can't instance it, you just use it such as `Math.pi()`. With a singleton class, you would still need to do something like singleton firstSingleton = new singleton(); firstSingleton.set_name("foo"); singleton secondSingleton = new singleton();Correct me if i am wrong, but `firstSingleton == secondSingleton` right now, yes? secondSingleston.set_name("bar"); firstSingleton.report_name(); // will output "bar" won't it?Please note, I am asking this language independently, more about the concept. So I am not worried about actually how to code such a class, but more why you would wan't to and what thing you would need to consider.

1 Expert Answer

By:

Patrick B. answered • 07/30/19

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.