title: Design pattern for managing groups of tasks
body: <p>I have a system that processes <code>Task</code> objects, and now I would like to perform some benchmarking experiments. Specifically, I will create many (~100) <code>Task</code> objects, each belonging to one <em>group of tasks</em>, 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 <code>Task</code> 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.</p>
  
  <p><code>Task</code>s can contain arbitrary <code>Objects</code>, so I can't load them from "data" file types like JSON -- only Java code is general enough to create the <code>Task</code>s. Furthermore, for maintainability, I'd like to create each <code>Task</code> in a separate Java file:</p>
  
  <pre><code>// 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";
      }
  }
  </code></pre>
  
  <p>Then, it should be easy to gather groups of <code>Task</code>s:</p>
  
  <pre><code>List&lt;Task&gt; tasks = gatherTasksInGroup("impossible-tasks");
  </code></pre>
  
  <p>without something silly and error-prone like:</p>
  
  <pre><code>List&lt;Task&gt; gatherTasksInGroup(String groupName) {
      List&lt;Task&gt; list = new ArrayList&lt;&gt;();
      if (groupName.equals("impossible-tasks")) {
          list.add(SolveHaltingProblem.getTask());
          list.add(SomeOtherHardProblem.getTask());
          ...
      } else if (...) {
          ... // ~100 more lines
      }
      return list;
  }
  </code></pre>
  
  <p>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 <code>SolveHaltingProblem</code> extend <code>ImpossibleTaskGroup</code> which extends <code>TaskGroup</code>...</p>
  
  <p>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 <code>SolveHaltingProblem</code>, and thus I can't force any static initializers of <code>SolveHaltingProblem</code> 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.</p>
  
  <p>So in summary, my question is: how can I manage groups of <code>Task</code>s so that it's easy to add a new <code>Task</code> and associate it with a group? A good design would have the following properties (ordered from greatest to least importance):</p>
  
  <ol>
  <li>Each <code>Task</code> is created in a separate file</li>
  <li>Adding a new <code>Task</code> and associating it with a group only involves adding/changing one file</li>
  <li>We only construct <code>Task</code> objects for the requested groups</li>
  <li>Creating a new group is "easy" by copy/pasting infrastructure for an existing group with minor modifications</li>
  <li>We don't iterate over all classes in the classpath, or iterate over files in the filesystem</li>
  </ol>