Design pattern when you have multiple tasks with and without dependency on each other

Viewed 198

for eg : say you are loading an app

Where you have tasks like login task - which has no dependency then say fetchUserData - which depends on login task then say fetchUserHistory etc - which say depends on above 2 tasks and say there are 10 more such tasks with cross dependencies

i know that I can simple do await async and run multiple independent etc parallely

but is there a generic way which is more neat and easy to add more tasks in future if required.

or is there a generic design pattern which does the same ?

1 Answers

You can always take a quick look at all design patterns out there and figure out if there is one which matches your use-case (they're not that many).

In order to make this code scalable, you should design tasks and dependencies in an abstract way, a minimal sketch would be something like:

interface Task {
    Result execute(Input input);
    List<Task> getDependencies();
}

Maybe Mediator pattern can help here, and also if you're looking for an execution engine for those tasks, you should consider a DAG-based scheduling process

Related