Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapping UML relationships to java

Tags:

java

uml

How can we map basic UML relationships like Aggregation, Dependency, Association to Java classes? How does this actually work?

like image 370
Mohit Sehgal Avatar asked Oct 06 '22 06:10

Mohit Sehgal


1 Answers

A dependency can be anything, so one can't tell how this is mapped to Java. It could be a plain usage of a class as local variable type; possibly even a dependency that does not appear in the code at all.

An Association is much more concrete. It is usually implemented by an instance variable (attribute/field) in the class which is referencing the other one. If it is a bidirectional Association the other class has a corresponding attribute. If the upper bound is 1 the corresponding role is a plain Java field. If greater than one a collection is used. Another implementation scheme would be using an extra class which has a collection of link tuples. For bidirectional Associations you might want to investigate for "referential integrity" as well.

An Aggregation is a special kind of Association. The difference in code usually is that the aggregate usually get it's parts already at construction time (e.g. in the constructor).

You can experiment with implementation styles (so called CodeStyles) e.g. with UML Lab - it allows you to see different Association implementations (Note: I'm an employee of the vendor of UML Lab). There are numerous other UML tools which generate code for Associations, of course.

Reading a book about it - as AmitD suggested - wouldn't hurt, as Associations can become quite complex.

like image 111
Christian Avatar answered Oct 10 '22 03:10

Christian