Creating custom classes for list storage
When is it acceptable to do this. For example, I find myself sometimes needing to create, say, a form of tuple or something that has a key
so like a
String -> (myObj1, myObj2, myObj3)
I end up making a class t开发者_运维知识库o hold the myObj1 -> 3
but as you can see this class has limited fields so it seems like its a waste of a class as such.
Should I not worry about that or is it bad design to create classes for list storage purposes?
It depends on how complicated the list objects are. Most languages/frameworks have build in classes for tuples
, pairs
, key->value pairs
, points
, associative arrays
and similar forms. If your objects are a little more complicated and can't find anything that fits your needs use a custom class. I don't see any problems here.
There are various options:
C# KeyValue: IList<KeyValuePair<string, string>>
C# Dictionaty: Dictionary<string, string>)
Java: Map<String, String>
Php associative arrays: $myVal = $myArray['$myKey']
精彩评论