DRY, @Peter Lawrey , , . ? - . factory . Java , :
One of the advantages of static factory methods is that, unlike constructors, they have names.
Thus, using static factory methods, your code will look like this:
public class Apples {
String color;
int quantity;
private Apples(String color, int quantity) {
this.color = color;
this.quantity = quantity;
}
public static Apples newInstance(String color, int quantity) {
return new Apples(color, quantity);
}
public static Apples newEmptyInsatnce(String color) {
return new Apples(color, 0);
}
}
Thus, the Apples.newEmptyInstance () method is more informative about what it creates than just an overloaded constructor. I just assumed why your professor said that it was a bad idea.
source
share