Reputation: 1131
I have tasks of different types stored in the DB. I need to fetch the tasks from DB and run each task in its appropriate service (e.g. AService handles ATask objects, BService handles BTask objects etc.). There is also a TaskController class which coordinates the execution of tasks.
Which is the better design -
1.Have the controller fetch all active tasks and send each task to the appropriate service.
2.Have each service fetch and execute all the tasks that are relevant to it (in this case the controller would have to loop over all the defined services and activate them).
Upvotes: 0
Views: 305
Reputation: 340733
I would go for solution 1. with the exception that an intermediary service should be created to handle what you want to put in the controller - loading and dispatching tasks.
Also I smell big fat switch
or a cascade of if
s, maybe with instanceof
. I guess you can benefit from chain of responsibility pattern - ask first service whether it can handle the task. If it can't - pass to the second one and so on.
Another approach would be a visitor if each task is a separate subclass of common class.
Upvotes: 1