views:

43

answers:

3

I have to "translate" codes with a conversion table like this:

| symbol | translation |  
|   1    |      3      |
|   2    |      4      |
|   3    |      6      |
|   4    |      5      |
|   5    |      2      |
|   6    |      1      |
|   7    |      1      |

My first idea was to use a Map associating each symbol to its translation and to load the table from a database or a text/xml file. Is there a better way? Doesn't have to be lightning fast, just easy to maintain and test. TIA.

+1  A: 

Loading a Map sounds ideal then. Easy to maintain and test (you can forgo the database in a unit test if you abstract out the database or XML stuff and use a mock to provide test values that aren't going to change).

David M
+1  A: 

I would go with the map approach as well. I think that's the simplest, thus easiest to maintain and test.

Péter Török
+1  A: 

Map is ideal unless your mapping table/file may change after you have loaded it in the Map.

In other words, if your association is fairly static, and can accept having to restart the application when it changes, go for a simple map.

Otherwise you have to think of some kind of notification mechanism so that the map can be updated (or even just reloaded) without relaunching the app.

Depending on the situation, you may want to expose a simple external call to refresh it, or poll the underlying file/table at regular intervals, or some combination of these.

p.marino