How to convert/translate information?
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.
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.
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).
I would go with the map approach as well. I think that's the simplest, thus easiest to maintain and test.
精彩评论