Normally, I would solve this via something like the following:
task = _taskProvider.getNextFromQueue();
if (task.id == TaskExecutor.TasksEnum.One) {
_taskExecutor.executeTaskOne();
return;
} else if (task.id == TaskExecutor.TasksEnum.Two) {
_taskExecutor.executeTaskTwo();
return;
} else if (task.id == TaskExecutor.TasksEnum.Three) {
_taskExecutor.executeTaskThree();
return;
}
//and so on...
I could switch if-else
to switch
if needed. I do believe there exists a better way to implement similar code. One thing that comes to mind is to use a table (map) to store task ids and pointers to corresponding functions, but I am not sure if it's useful and provide adequate performance.
Another thing that concerns me is notification mechanism. This problem occurs when TaskProvider
and TaskExecutor
are two separate entities that operate on different threads. It becomes even worse if several actions take some time to finish execution and I need to create a logic to make a caller wait for the result.
For example, if executeTaskTwo()
takes some time to complete, I would execute it in a background thread, meaning executeTaskTwo()
would return nearly immediately. But how do I notify the caller about it's completion?
I guess that the solution involves heavy modification, maybe some event libraries (I've never worked with these before). I need a starting point. I guess there exist a patterns of some sort, but I do not know which one exactly.
Aucun commentaire:
Enregistrer un commentaire