mardi 20 septembre 2016

Algorithm/Software Design to take an Action based on a Test performed on an Item

I'm trying to avoid using an if/else tree or switch statement in my program and I came here for some software-engineering-if-you-will advice.

I welcome a solution in an language/data structures/design/pseudo-code!

Here goes:

You have a list of Items which you want to perform an Action. There are several choices of Actions, the Action to take can be determined using a Test, there are also several choices of Tests.

Approach I am trying to avoid/improve upon:

For Each Item in Items:
   if Test1 on Item passes
      perform Action1
   else if Test2 on Item passes
      perform Action2
   else if Test3 on Item passes
      perform Action3
....
etc

The other querk which might make these even more difficult, would be if said Actions required different parameters, but I believe that could be solved using perhaps a parameter object, and then only retrieving/using the elements needed or thought optional parameters or parameter lists?

Aucun commentaire:

Enregistrer un commentaire