Search code examples
javahashmapumlclass-diagram

Represent hashmaps using an UML class diagram


I'm new to UML. Does this class diagram represent correctly a Hashmap<String, Hashmap<String, String>> structure?

enter image description here

More precisely, the config property must be a struct like a Hashmap<String, String> (Java) or dict[str, str] (Python) and the students object must be a Hashmap<String, Hasmap<String, String>> (Java) or in Python dict[str, dict[str, str]] for instance. How to represent this structure (data types of the properties) using an uml class diagram?

What I tried to do in my diagram: I've two times M because there is a class M and a class M2D which are used both. M2D maps a key of type String to M. M maps a key of type String to a String. In my understanding M2D represents Hashmap<String, Hashmap<String, String>>(Java Implemention) since M represents Hashmap<String String>.


Solution

  • What's the problem?

    Reading your additional explanation in the comments, I understand that you try to have a very dynamic implementation, with a school having several dynamic configuration parameters, and students represented by a set of dynamic properties:

    • In your implementation, these dynamic structures are easily and efficiently dealt with as hashmaps.
    • However, in practice, one gets quickly lost by what is which hash map supposed to represent. Now you're comfortable with it, but if you do another project and come back in 6 months you will understand what I mean.

    The problem that you face in the modelling is that UML aims to help you provide clarity on your design, and not to visually program what's already in the code.

    How to solve it?

    I understand that the config in your School is in fact a set of config parameters, each identified by a unique key for a given school:

    The config property must be a struct like a Hashmap<String, String> (Java) or dict[str, str] (Python)

    There are two approaches, both with config removed from the school attributes:

    • use a qualifier (the small rectangle with a qualifier attribute Key:String) directly for the School, and associate it with a String, or better with a ConfigurationParameter which would be a string, but could evolve into something more complex later. This would be very close to your implementation.

    • abstract from the way you implemented your configuration, and associate one school to many ConfigurationElements, which would be a class with a property key, and a property parameter both being strings. This would allow other implementations as just the Hashmap.

    For the more complex Hashmap<String, Hasmap<String, String>>, it's more challenging:

    the students object must be a Hashmap<String, Hasmap<String, String>> (Java) or in Python dict[str, dict[str, str]] for instance.

    First of all, I see a School class and no Student class. But I see that you use a students property and note the plural. And you use a type M2D which we see you try to associate with a qualifier to something. So I think that your design forgets to show the design intent. If the school has many students, then associate your school with a Student class. Again:

    • you could use a qualified association (e.g. if a student ID allows to find exactly one student for a school). I'll use this in my diagram
    • or you coud use a simple one to many association. This seems a better approach if the school acts as a kind of repository for the students, and if beyond a getSudentdById(...):Student you'd also want dome more creative queries, e.g. getStudentsByAge(...):Student[*]

    Now, if your Student is implemented as a set of pair values (e.g. you don't want properties of a student to be fixed, but dynamic), then associate the Student with a qualifier to the StudentAttribute class which is a string.

    The advantage is that this modelling makes the navigation in the Hashmap<String, Hasmap<String, String>> much clearer: the first string gives a student, the second string gives an attribute of a student which is a string. enter image description here Of course, you could remove the intermediates and associated directly to string, like the hashmap do, but then you should at least add a comment to explain what the association with String is supposed to convey as information.