1

I am working on a project where the input is a map of key-value pairs. Different users can provide different key-value pairs in the input. But the keys are pre-defined and maybe even an Enum. Based on the presence of a key, I need to execute some logic. For example, if the map contains key A, then I need to execute logic x1. If map contains B then execute logic x2, and so on. I was thinking of using the command pattern, where for each key, there is a corresponding command. So if I find A and B in the map, I add both x1 and x2 to my list and once I'm done iterating through all items of the map, I start executing everything in that list.

What do you think of this? How would you approach this problem?

MadN
  • 47
  • 3
  • Possible duplicate of [Style for control flow with validation checks](https://softwareengineering.stackexchange.com/questions/148849/style-for-control-flow-with-validation-checks) – gnat Sep 28 '17 at 07:32
  • 6
    It is OK as long as `x1 ; x2` has same result as `x2 ; x1`, unless order of key-value pairs can be specified in your input map structure. – mouviciel Sep 28 '17 at 07:46
  • 3
    The solution you're proposing seems fine, given the *very narrowly constrained* and *partially solved problem* you've posed. (If you want a better answer try posing a problem in domain terms instead of technological terms.) – Erik Eidt Sep 28 '17 at 16:45
  • The way the problem is stated, the user input is executed like a sequence of transactions, or a list of commands ... view the keys as tokens and the values as arguments and you are already half way down the road to a simple command interpreter i.e. sequential language, especially if order is important which might be depending on the individual commands ... where things can reordered you could start optimizing ... also look into tools like lex/yacc etc. – Gregor Ophey Sep 29 '17 at 06:23

0 Answers0