In my project which is kind of a tcp server, i need to keep track of a lot of objects to be used in a global scope. I got addicted towards hashtable and currently im using like 10 hashtables in my project which will store different kinds of objects.
My question is, is this alright? i know that its a pretty huge data strucre but it makes my work very easy, rather than going for some other alternative. For instance i have many groups of textboxes which i need to keep track of. so i put them all in a hashtable and use them as i like.
Is this kind of approach an acceptable and normal way to program when working on a large multithreade开发者_如何学Cd application.
It's not clear from your example, but it sounds as if you're using global hashmaps to access objects you need (such as textbox in an interface) from any point in your program.
a) Global access like that is not good practice, and even less so with interface objects. Those should be accessed only from the front-end module, your TCP server back-end couldn't care less about those.
b) The correct way of arranging objects in a "functional group" is having a proper Class, not 5-10 hashmaps. Why, in practice? Consider these two approaches (in pythonlike pseduo-code):
# Interface is a hash-map of interface objects, like text-boxes. 'status' is the key
# in the map for one of those.
Interface['status'].setText("New status!")
# vs:
# Interface is a proper object with its own class, that handles the interface. It has
# all our text-boxes "inside" of it.
Interface.updateStatus("New status!")
Now, say you change your mind and want to represent the status as a drawing, instead of a text-box. With the second approach that's easy: you just adjust the GUI accordingly, and change the way the updateStatus method behaves!
With the first approach you now have a mess of hahsmap accesses to a text-box that doesn't exist anymore, spread all over your code.
This is a much more general practice than just hashmaps or global objects -- it's about working with objects that have clearly defined interfaces, and that can change internally without the rest of the program being affected.
The great advantage of using hashtable is that you can store data of any type. Internally HashTable uses Dictionary Type.
If all the data you are storing inside the hashtable is the of the same type i would suggest you to use Generic type.
Dictionary < int, string > or Even List<,> type for better performance.
I see no issue with it as long as your memory consumption it low. Afterwards you should migrate The hash table to disk. Either use a full flown relational database or simply use one of the key value stores. Your choice.
精彩评论