How to name this class? Is it a pattern?
https://softwareengineering.stackexchange.com/questions/323197
-
19-12-2020 - |
Pergunta
I need to create a class which will have the following structure:
public class MyClass {
ArrayList<File> myList = new ArrayList<>();
public void addFile(File f) {
myList.add(f);
}
public void DoHeavyWork() {
for (File f: myList) {
/// ...
}
}
}
I was thinking on this as maybe a Facade or maybe some kind of container. Would that be correct?
Solução
It seems like a batcher. So you could name it like this.
public class HeavyWorkBatch {
ArrayList<File> myList = new ArrayList<>();
public void AddToBatch(File f) {
myList.add(f);
}
public void DoHeavyWork() {
for (File f: myList) {
/// ...
}
}
}
// Note there is a conspicuous lack of cleanup...
// What happens when AddToBatch is called again after DoHeavyWork?
// Is the class reusable so that DoHeavyWork should clear the ArrayList?
However, having the collection internal is not necessary based on the sample given. And given the questions I posed above, you could simplify it to just this:
public static void DoHeavyWork(File f) {
/// ...
}
And then build the list separately, and iterate separately from doing the work. That way, your file operation can be reasoned about and reused separately from the collection. Because building a batch is a separate concern from operating on a file.
myList.forEach((file) -> {
HeavyWork.DoHeavyWork(file);
});
If you need to build a batch from a larger list, then you could do so in other ways.