title: Elegant design for workflow with feedback mechanism
body: <p>I'm looking for a way to model my Java application code as good as possible, but I can't wrap my head around a good design.</p>
  
  <p>I'm trying to import data, process it into a database and send feedback afterwards. I'll also save the state of the import process as an entity in a database, because feedback about the process is an important factor. I came up with this design, but I don't feel good about it.</p>
  
  <pre><code>public interface FeedbackService {
  
      void startingImport();
  
      void fetchingDataSuccessful(int amountObjectsFetched);
  
      void fetchingDataUnsuccessful(Exception e);
  
      void addSuccessfulObjectProcess(Object processedObject);
  
      void addUnsuccessfulObjectProcess(Object processedObject);
  
      void processingDataSuccessful(int amountObjectsProcessed);
  
      void processingDataUnsuccessful(Exception e);
  
      void sendFeedback();
  
      void finishImport();
  }
  </code></pre>
  
  <p>A big concern I have about this design, is that you have different states in which only certain functions can be called. I read somewhere that it's terrible design when your methods depend on calling other methods first. Also, this would call for the State design pattern, which might be overkill for what is basically an extended logging library.</p>
  
  <p>I tried to solve this problem by using the Template pattern, that way the workflow and the logging with their sequential dependencies are handled by the super class, while the sub classes can handle the data specific code (processing products, processing catalogs, etc). </p>
  
  <pre><code>public void handle(){
      registration.startImport();
  
      try {
          int amount = fetchData();
          registration.fetchingDataSuccessful(amount);
          try {
              for (Object o : getData()) {
                  if (handleObject(o)) {
                      registration.addSuccessfulObjectProcess(o);
                  } else {
                      registration.addUnsuccessfulObjectProcess(o);
                  }
              }
              registration.processingDataSuccessful(getData().size());
  
          } catch (DataHandlingException e) {
              registration.processingDataUnsuccessful(e);
          }
  
      } catch (DataFetchingException e) {
          registration.fetchingDataUnsuccessful(e);
      }
  
      registration.sendFeedback();
  }
  </code></pre>
  
  <p><em>fetchData(), getData() and handleObject() would be abstract methods implement by the sub class</em></p>
  
  <p>However, now my code leaks, especially with the add(Un)successfulObjectProcess method and the getData() method. Since this one needs to be executed inside the loop, I need to pull the loop (and the data) to the super class, or, alternatively, I need to pass the feedbackservice into the sub class. Both ways are not really what I want. Especially since the handling of a list of objects might be more complicated than looping once over every element. (sometimes we need to fetch references, build trees, etc)</p>
  
  <p>I feel like I'm over complicating this design, but I don't want to write one long spaghetti function to just make it work. I'm looking for ideas, previous experiences and feedback about how this should be tackled. </p>