mercredi 26 avril 2017

java design pattern for for decoupling task completion and execution of next

I implemented a wrapper for a queue that holds incoming tasks. Every time a task is executed, a callback is called to the wrapper's owning class for the the actual execution (doSomething()).
This implementation doesn't look good, because as long as there are still items in the queue, the callbacks will start chaining together and grow the stack. Is there a common design pattern to decouple the completion of the previous task and execution of the next task?

    public void executeCallback(Task task) {
      switch(task.type()) {
        case TASK_TYPE1:
          doSomething();
          break;
        case TASK_TYPE2:
          doSomethingElse();
      }
      queueWrapper.executeNextTaskInQueue(); //doesn't look too good
    }
}

Aucun commentaire:

Enregistrer un commentaire