Saturday, April 18, 2009

Roles in associations in class diagrams

Came across this:


I noticed the line which said
When code is generated for class diagrams, named association ends become instance variables in the target class.


So naturally I tried to plug this into Visual Paradigm, and after fiddling around with it for a while I got them to appear with the right syntax. Then I pressed the generate code button, and voila - everything done in a jiffy.

So all you have to do really is to notate the multiplicity on either end of the association (or aggregation, or composition), then add a role name to the child class, then set the visibility of the role (usually "-" for private). Once this is done, you have told VP all it needs to know to generate the code, complete with ArrayLists and everything.

VP UML screenshot for adding roles in class diagrams




In an unrelated note, I have discovered an annoying little bug in VP, where you have a class that inherits from a parent class, the Java code it should convert to is
public class Child extends Parent {...

However, for some weird reason, the code it outputs (sometimes, and sometimes not), is
public class Child extends Parent implements Parent {...

Which doesn't make sense, because you would never be in a situation where one class would need to extend and implement the same class at the same time.

No comments:

Post a Comment