I have a method that needs to execute multiple tasks to achieve a bigger task. Each task could be around 20-30 lines of code, so I decided to have a class per task.
public void bigTask() {
TasksExecutor executor = new TaskExecutor();
executor.addTask(new Task1(some arguments here));
executor.addTask(new Task2(some other arguments here));
executor.addTask(new Task2(some other arguments here));
executor.run();
}
public interface Task {
public void execute();
}
public class Task1 implements Task {
@Override
public void execute() {
//Some code here
}
}
public class Task2 implements Task {
@Override
public void execute() {
//Some other code here
}
}
public class Task3 implements Task {
@Override
public void execute() {
//Some other code here
}
}
public class TasksProcessor implements Serializable {
private List<Task> tasksList;
public TasksProcessor() {
this.tasksList = new ArrayList<Task>();
}
public void addTask(Task task) {
this.tasksList.add(task);
}
public void execute() {
for (Task task : this.tasksList) {
task.execute();
}
}
}
For me, this code is like a command pattern, but I am not sure because the arguments for each task are of different types, unlike the traditional command pattern.
Do you think this could be considered a command pattern implementation? Do you think this approach is OK for splitting a big method?
Thank you
Aucun commentaire:
Enregistrer un commentaire