I’m in the design stages of a program that based upon input received, would select a particular category, and action within that category. The category, the action within in, and the value(s) related to that action are all immutable values, along with the relationship between category and action.
Normally I’d just assign it all into a multidimensional array or collection, but that strikes me as both messy and unnecessarily complex to search and sort. Constants don’t allow me the luxury of defining relationships between them (aka you can’t have constant A bound to subConstant B for example), and being a desktop app, having a user setting up a database server isn’t appealing. Was thinking of having everything in xml files, but core rules of the application isn’t appealing due to being able to easily edit, and hence change the primary functionality of the program that I wouldn’t wish to be editable.
Anyone ever ran into something like this, and if so, what was the setup that you used for your solution?
EDIT: Let me see if I can explain this better with an example. Say I have the following structure
category A
-- action AB
-- result 1
-- result 2
-- result 3
-- result 4
-- result 5
-- result 6
where if I selected category A, and action AB, a one dice roll would select the result to be returned. The category, action, and result will never change both in name and in relationship (action AB will always be bound to category A, results within action AB will be bound to action AB, and the result contents will never change)
EDIT: No network usage would be expected for this application, as it was asked in one of the responses.
3
Create classes or interfaces for categories, actions, and results. Start with a list for categories. (You’ll need random access, so this should perhaps be a dictionary/map, though if you can ID them with integers an ArrayList might do.) Each Category will have a similar list of Action instances, and each Action will have a list of Result instances. (Actually, if these are interfaces rather than instances, each instance can have completely different code from every other, as best suits its purpose, and the lists can be different, or even nonexistent. You might choose results with an IF statement in one Action and an array in another.)
Now you create a new Result by creating a new instance of a subclass of the desired class, which implements the necessary interface. (For simple cases you can skip a step or two.) You create an Action similarly, then add Results to it. And a Category the same, and add Actions to it.
The key point here is the flexiblity. You can do just about anything and everything. Arrays in one place, switch statements in another, if-elses somewhere else. Also string-based dictionaries, etc., etc. While each Category looks and works, to outsiders, identical to every other, internally they can be completely different.
(Of course, for the simplest cases a simple nested if-else construct might do the whole job just as well.)
Even though the question is a little bit fuzzy, I can try to propose a solution.
As already mentioned by Neil, tree-like data structure can be useful here. I guess, if changing values is not needed (those are constants?), then it’s just a matter of not exposing them to the end user.
The lightweight solution could be binary serialization of hashes (objects, dictionaries, associative arrays – terminology depends on your language/framework/platform). If you need to keep them apart from the program, there are simple and efficient key-value storages (like Berkeley DB). It’s possible to use SQL-based approach even with sqlite – if I am not mistaken, even Firefox browser uses one under the hood: No database server needed. With sqlite, even full text search is possible. Sqlite works well even with in-memory backend, in case this is what is needed.
What is even better, user preferences can be also stored in the sqlite, in a different file.
Nowhere there is a mention that those constants need to be shared over the network, but even then serialization approach can be used.