简体   繁体   中英

mapping UML relationships to java

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

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 (eg in the constructor).

You can experiment with implementation styles (so called CodeStyles) eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM