Here's a topic I don't understand. What's the benefit of making a private constructor and a public getInstance method?
I understand why you would make the constructor private: to keep someone from instantiating a class. The correct term is "Utility class", right? The example I get is the Math class. Programmers should have access to Math.sqrt(), Math.random() and other methods, but they should never be able to write "Math myObject = new Math();"
But why make a public method getInstance()? Take the Calendar object, for example. You can't write "Calendar c = new Calendar()". You have to write "Calendar c = Calendar.getInstance()". But it has the exact same result: an object of type Calendar with a reference named "c".
Some research pointed me toward the "Factory Design Pattern," explained here. The concept is that you create multiple "getInstance" methods that return Objects with different properties based on the parameters of the method call. But couldn't you achieve the same objective with overloaded constructors?
Further research pointed me toward "Singletons", which ensure that only one instance of a class is instantiated. I can see a scenario where you'd want that. The getInstance() method checks to see if an object of that class already exists in the program. It returns null if an object exists. Couldn't you build that logic into the constructor?
I'm definitely missing something here. The paragraphs aren't arguing against getInstance methods. There to demonstrate that I've researched the topic and still don't get it. Can a friendly Arsian give me an example where a getInstance method works better than a public constructor(s)?
I understand why you would make the constructor private: to keep someone from instantiating a class. The correct term is "Utility class", right? The example I get is the Math class. Programmers should have access to Math.sqrt(), Math.random() and other methods, but they should never be able to write "Math myObject = new Math();"
But why make a public method getInstance()? Take the Calendar object, for example. You can't write "Calendar c = new Calendar()". You have to write "Calendar c = Calendar.getInstance()". But it has the exact same result: an object of type Calendar with a reference named "c".
Some research pointed me toward the "Factory Design Pattern," explained here. The concept is that you create multiple "getInstance" methods that return Objects with different properties based on the parameters of the method call. But couldn't you achieve the same objective with overloaded constructors?
Further research pointed me toward "Singletons", which ensure that only one instance of a class is instantiated. I can see a scenario where you'd want that. The getInstance() method checks to see if an object of that class already exists in the program. It returns null if an object exists. Couldn't you build that logic into the constructor?
I'm definitely missing something here. The paragraphs aren't arguing against getInstance methods. There to demonstrate that I've researched the topic and still don't get it. Can a friendly Arsian give me an example where a getInstance method works better than a public constructor(s)?