Mapping UML Models to Java Code
Mapping UML Models to Java Code
Refactoring
Model
transformation
Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space
User
+email:Address
Refactoring
Model
transformation
Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space
Lesson 3: Mapping Model to Code 7
Refactoring Example: Pull Up Field
public class Player { public class User {
private String email; private String email;
//... }
} public class Player extends User {
public class LeagueOwner { //...
private String eMail;
}
//...
} public class LeagueOwner extends User {
public class Advertiser { //...
private String email_address; }
//...
public class Advertiser extends User {
}
//...
}
Refactoring
Model
transformation
Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space
Lesson 3: Mapping Model to Code 10
Forward Engineering Example
Object design model before transformation:
User LeagueOwner
-email:String -maxNumLeagues:int
+getEmail():String +getMaxNumLeagues():int
+setEmail(e:String) +setMaxNumLeagues(n:int)
+notify(msg:String)
Person SocialSecurity
number:String
Person
SSN:String
image
ImageProxy RealImage
1 0..1
filename:String data:byte[]
paint() paint()
Lesson 3: Mapping Model to Code 15
Examples of Model Transformations and
Forward Engineering
• Model Transformations
• Goal: Optimizing the object design model
• Collapsing objects
• Delaying expensive computations
• Forward Engineering
• Goal: Implementing the object design model in a programming language
• Mapping inheritance
• Mapping associations
• Mapping contracts to exceptions
• Mapping object models to tables
* +selectSponsors(advertisers):List *
+advertizeTournament()
+acceptPlayer(p)
+announceTournament()
+isPlayerOverbooked():boolean
1
1
Tournament
-maNumPlayers:String
* +start:Date *
* * players +end:Date sponsors * *
Player +acceptPlayer(p) Advertiser
+removePlayer(p)
* +isPlayerAccepted(p)
matches *
matches
Match
*
+start:Date
+status:MatchStatus
+playMove(p,m)
+getScore():Map
Lesson 3: Mapping Model to Code 29
TournamentForm
1
1
+applyForTournament() TournamentControl
1
Tournament
-maNumPlayers:String
* +start:Date *
* * players +end:Date sponsors* *
Player +acceptPlayer(p) Advertiser
+removePlayer(p)
* +isPlayerAccepted(p)
matches*
matches
Match
*
+start:Date
+status:MatchStatus
+playMove(p,m)
+getScore():Map
public class TournamentForm {
private TournamentControl control;
private ArrayList players;
public void processPlayerApplications() {
for (Iteration i = [Link](); [Link]();) {
try {
[Link]((Player)[Link]());
}
catch (KnownPlayerException e) {
// If exception was caught, log it to console
[Link]([Link]());
}
}
}
} Lesson 3: Mapping Model to Code 30
The try-throw-catch Mechanism in Java
public class TournamentControl {
private Tournament tournament;
public void addPlayer(Player p) throws KnownPlayerException {
if ([Link](p)) {
throw new KnownPlayerException(p);
}
//... Normal addPlayer behavior
}
}
public class TournamentForm {
private TournamentControl control;
private ArrayList players;
public void processPlayerApplications() {
for (Iteration i = [Link](); [Link]();) {
try {
[Link]((Player)[Link]());
}
catch (KnownPlayerException e) {
// If exception was caught, log it to console
[Link]([Link]());
}
}
}
} Lesson 3: Mapping Model to Code 31
Implementing a Contract
• Check each precondition:
• Before the beginning of the method with a test to check the precondition for that
method
• Raise an exception if the precondition evaluates to false
• Check each postcondition:
• At the end of the method write a test to check the postcondition
• Raise an exception if the postcondition evaluates to false. If more than one postcondition is
not satisfied, raise an exception only for the first violation.
• Check each invariant:
• Check invariants at the same time when checking preconditions and when checking
postconditions
• Deal with inheritance:
• Add the checking code for preconditions and postconditions also into methods that
can be called from the class.
Tournament
«precondition»
!isPlayerAccepted(p) -maxNumPlayers: int
+getNumPlayers():int
+getMaxNumPlayers():int
+isPlayerAccepted(p:Player):boolean
+addPlayer(p:Player)
«precondition» «postcondition»
getNumPlayers() < isPlayerAccepted(p)
getMaxNumPlayers()
Student
Collapse or not to
Matrikelnumber:String
collapse?