I have a class which has multiple constructors. Each represent different use cases.
public class ABC {
public ABC(int x) {
...
}
public ABC(ArrayList<String> Stringarray) {
...
}
..many more constructors..
}
Constructor overloading was the clean solution so far until I encountered same erasure issues from java compiler. For example, I want to add another constructor which end up having same erasure, so I just chose to include a default parameter to work around for now like below:
public ABC(ArrayList<String> stringArray) {
…
}
public ABC(ArrayList<Integer> integerArray, boolean… sameErasureFlag) {
…
}
But I have a strong feeling probably having so many constructors is not a good design pattern for this use case.. Maybe there is a better solution or best practice design pattern that is used for such a scenario. I am looking up on the builder pattern, but not sure if that is right/better one. Any recommendations?
Aucun commentaire:
Enregistrer un commentaire