mardi 3 avril 2018

Design pattern for accessing groups of tasks

I have a system that processes Task objects, and now I would like to perform some benchmarking experiments. Specifically, I will create many (~100) Task objects, each belonging to one group of tasks, and I want to run the system on entire groups of tasks. I'd like a design that makes it easy to create a new Task and associate it with a group (easily diversify the benchmark suite). There are only a handful of groups, so some per-group infrastructure is acceptable.

Tasks can contain arbitrary Objects, so I can't load them from "data" file types like JSON -- only Java code is general enough to create the Tasks. Furthermore, for maintainability, I'd like to create each Task in a separate Java file:

// SolveHaltingProblem.java
public class SolveHaltingProblem {
    static Task getTask() {
        Task task = new Task();
        task.setName("SolveHaltingProblem");
        ... // Create task
        return task;
    }

    static String getGroup() {
        return "impossible-tasks";
    }
}

Then, it should be easy to gather groups of Tasks:

List<Task> tasks = gatherTasksInGroup("impossible-tasks");

without something silly and error-prone like:

List<Task> gatherTasksInGroup(String groupName) {
    List<Task> list = new ArrayList<>();
    if (groupName.equals("impossible-tasks")) {
        list.add(SolveHaltingProblem.getTask());
        list.add(SomeOtherHardProblem.getTask());
        ...
    } else if (...) {
        ... // ~100 more lines
    }
    return list;
}

I provide the above code just to help communicate my needs, and the design details aren't set in stone. Maybe it's better to have SolveHaltingProblem extend ImpossibleTaskGroup which extends TaskGroup...

I'm aware of the pattern where classes register themselves with other classes (is there a name for this?), but I don't think that pattern applies here since I'm not creating instances of SolveHaltingProblem, and thus I can't force any static initializers of SolveHaltingProblem to run. I've also tried to search StackOverflow for similar questions, but I've found that this question is quite hard to describe; I apologize if this is a duplicate.

So in summary, my question is: how can I manage groups of Tasks so that it's easy to add a new Task and associate it with a group? A good design would have the following properties (ordered from greatest to least importance):

  1. Each Task is created in a separate file
  2. Adding a new Task and associating it with a group only involves adding/changing one file
  3. We only construct Task objects for the requested groups
  4. Creating a new group is "easy" by copy/pasting infrastructure for an existing group with minor modifications
  5. We don't iterate over all classes in the classpath, or iterate over files in the filesystem

Aucun commentaire:

Enregistrer un commentaire