0

I need suggestions about the right choice of a design pattern.

I have three kind of tasks (but could be more in future) TaskA, TaskB, TaskC.

The controller receive some params and have to perform TaskA and TaskB if the param X has value major to 10, or TaskA, TaskB and TaskC if the param X has value equal or minor 10.

The goal is to be able to add all tasks with his own condition, in this case we could add easily other task or other conditions (i.e. TaskD if param Y has a certain value).

Is Chain of Responsibility the right Design Pattern to adopt? Should I use other patterns join to this one?

Zauker
  • 2,344
  • 3
  • 27
  • 36
  • It sounds like you want one method to take any params and perform any actions. That's very abstract. My advice is, don't do that. Separate logic into small, single-purpose methods that operate on few params. – jaco0646 Feb 18 '21 at 14:16
  • maybe there is not enough going on to justify a design pattern. maybe it's just a series of if/then's. – Ray Tayek Feb 19 '21 at 12:59

1 Answers1

0

I would define tasks as Commands (see the Command pattern), implementing a common executable interface (with an execute() method). Each command could be instantiated specifying its own parameters, so it could evaluate them by itself and skip its execution if needed.

To execute commands I would loop on the task list invoking the execute method on all the items.

Chain of Responsibility could be a good choice if a task can autonomously stop the execution of the others, otherwise it's not needed here IMHO.

CptWasp
  • 459
  • 2
  • 13