0

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
  • 2
    It's not clear what you mean. Why wouldn't a tree data structure work here?
    – Neil
    Sep 13, 2013 at 14:43
  • @neil I was thinking that it would be the way to go originally, but the only way that I knew how to implement such a structure would be to have it called from an external data source, which would be cumbersome in a standalone desktop app, and I dind't know what would be a good way to do so while having it imbedded within the app itself. Sep 13, 2013 at 14:59
  • 1
    1- The data structure used to hold this knowledge in memory and the permanent storage are independent. 2- If you want this structure to be hardcoded and not user-editable, why not build it in the source code itself?
    – Andres F.
    Sep 13, 2013 at 15:04

2 Answers 2

1

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.)

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.