I have a method as follows (simplified for clarity and brevity):
public static List<Person> getPersons(String a, int b, DataSetX dataSetX, DataSetY dataSetY) {
List<Person> persons = new ArrayList<>();
if(dataSetX != null) {
while (dataSetX.hasNext()) {
// create an instance of person with very specific attributes set
persons.add(processX(dataSetX.next()));
}
}
if(dataSetY != null) {
while (dataSetY.hasNext()) {
// create an instance of person with very specific attributes set
persons.add(processY(dataSetY.next()));
}
}
return persons;
}
The method in reality is a bit more complicate than this (doing a bit more processing using also the a
and b
variables) but overall this is the method structure.
I was thinking to split this method into 2, one method for dealing with DataSetX
and the other with DataSetY
.
I was thinking to structure it as follows:
public static List<Person> getPersons(String a, DataSetX dataSetX, List<Person> persons) {
if(dataSetX != null) {
while (dataSetX.hasNext()) {
// create an instance of person with very specific attributes set
persons.add(processX(dataSetX.next()));
}
}
return persons;
}
I would then call the methods as follows:
List<Person> persons = getPersons(a, dataSetX, new ArrayList<Person>());
getPersons(a, dataSetX, persons);
// now I can use persons list with the result of both present
With this approach I reuse the same list and don't need to concat 2 different lists from 2 different methods if I just created the list inside the methods and returned.
On the other side it looks kind of weird and possibly error prone.
Is there a way to be able to split the function and avoid creating multiple lists and merging them (as I need 1 list in the end).
Is there some design pattern suited for this?