vendredi 10 novembre 2017

Java Method construction for repeated structure with small changes

I'm not sure if there's a pattern that covers this question, but here goes. In writing a method to call an analytical algorithm (such as a parser or calculation implementation), there is quite a large amount of code to read a source, apply the algorithm, then convert the results to something useful. Basically theres 20-30 lines of code and one algorithm/parser/tokenizer that changes quite often.

So.... my options that I see so far are:

  1. Create a new method for each algorithm (ugly due to repetition)
  2. Create a single method and pass in the algorithm as a parameter, then use a case or if/then to select the algorithm (this gets messy because I have to hard code my algorithm choices.
  3. Create a separate initialization or setting of the algorithm and then check to see if its initialized in the method (which still is ugly, because somewhere else I have a hard-coded list of different algorithm choices in my selection method).

Is there a neat trick or general method construction programming pattern to solve this?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire