From diagram to code
Turning a class diagram into Java code: visibilities, attributes, an association becoming an attribute or a collection depending on the multiplicity, navigability, generalisation, realisation and composition.
- 21 cards
- 4 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Understand
A few cards from the deck
Translating a visibility: what is it?
Show the answer
+becomespublic,-becomesprivate,#becomesprotected, and~has no keyword: you write nothing.Java's default level, with no keyword, is exactly package visibility.
In the diagram,
Equipeis joined toJoueurby an association navigable fromEquipetoJoueur, with0..*on theJoueurside. Which declaration goes inEquipe?private List<Joueur> joueurs = new ArrayList<>();private Joueur joueur;private List<Equipe> joueurs = new ArrayList<>();private static Joueur joueurs;
Show the answer
private List<Joueur> joueurs = new ArrayList<>();Zero or more players: the upper bound exceeds 1, so a collection of
Joueur, created empty so it is usable. A single reference can only hold one, andstaticwould share the attribute across all teams.An underlined member translates to …; an italic member, to ….
Show the answer
An underlined member translates to static; an italic member, to abstract.
An abstract member forces its class to be abstract too: the class name itself goes italic in the box.
Navigability: what is it?
Show the answer
The direction in which one object can reach the other, shown by an open arrow at the end of the line; only the starting object keeps a reference.
Order→Linegives an attribute inOrderand none inLine. With no arrow, the direction is left open, or both sides get an attribute.An association is navigable from
CommandetoLigneonly. Why does onlyCommandeget an attribute?- The arrow says which class keeps a reference to the other
- Java forbids two classes from referencing each other
- An attribute placed in
Lignewould have to be public - An attribute in
Lignewould cause an endless loop at construction
Show the answer
The arrow says which class keeps a reference to the other
A reference is only worth keeping if the code needs it. Adding an attribute in
Lignewould create a useless dependency and two references to keep consistent; two classes can of course reference each other when you decide so.