Lesson 8 Mapping Uml to Code
Lesson 8 Mapping Uml to Code
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2
Characteristics of Object Design Activities
Developers perform transformations to the object model to
improve its modularity and performance.
Developers transform the associations of the object model into
collections of object references, because programming
languages do not support the concept of association.
If the programming language does not support contracts, the
developer needs to write code for detecting and handling
contract violations. Self reading In C++? In Java?
Developers often revise the interface specification to
accommodate new requirements from the client.
All these activities are intellectually not challenging
However, they have a repetitive and mechanical flavor that makes
them error prone.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3
State of the Art of Model-based Software Engineering
The Vision
During object design we would like to implement a system that realizes
the use cases specified during requirements elicitation and system
design. Recall our discussion
The Reality
Different developers usually handle contract violations differently.
Undocumented parameters are often added to the API to address
a requirement change.
Additional attributes are usually added to the object model, but are
not handled by the persistent data management system, possibly
because of a miscommunication. And lack of traceability?
Many improvised code changes and workarounds are made ad hoc,
which eventually leads to a severe degradation of the system.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4
Model transformations
Forward
engineering
Refactoring
Model
transformation
Reverse
engineering
Model space Source code space
1-1 mapping?
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5
Model Transformation Example
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6
Refactoring Example: Pull Up Field
public class User {
private String email;
}
public class Player { public class Player
private String email; extends User {
//... //...
} }
public class LeagueOwner { public class LeagueOwner extends
private String eMail; User {
//... //...
} }
public class Advertiser {
public class Advertiser extends
private String User {
email_address;
//...
//...
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7
Refactoring Example: Pull Up Constructor Body
public class User {
public class User {
public User(String email) {
private String email; this.email = email;
} }
}
public class Player extends
User {
public Player(String email) public class Player extends
{ User {
this.email = email; public Player(String email){
} super(email);
} }
public class LeagueOwner }
extends User{ public class LeagueOwner
public LeagueOwner(String extends User {
email) { public LeagueOwner(String
this.email = email; email) {
} super(email);
} Bernd Bruegge & Allen H. Dutoit
}
Object-Oriented Software Engineering: Using UML, Patterns, and Java 8
Other Mapping Activities
Optimizing the Object Design Model
Collapsing objects
Delaying expensive computations
Mapping Associations
Mapping Contracts to Exceptions Self reading
Mapping Object Models to Tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
Collapsing an object without interesting behavior
Person SocialSecurity
number:String
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
Self reading
Delaying expensive computations
Object design model before transformation
Image
filename:String
data:byte[]
paint()
image
ImageProxy RealImage
1 0..1
filename:String data:byte[]
paint() paint()
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
Other Mapping Activities
Optimizing the Object Design Model
Mapping Associations
Mapping Contracts to Exceptions
Mapping Object Models to Tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12
Realization of a unidirectional, one-to-one association
Object design model before transformation Which direction?
1 1
Advertiser Account
Role name?
Source code after transformation
public class Advertiser {
private Account account;
public Advertiser() {
account = new Account();
}
public Account getAccount() {
return account;
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13
Bidirectional one-to-one association
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14
Bidirectional, one-to-many association
Object design model before transformation
1 *
Advertiser Account
complete?
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15
Bidirectional, many-to-many association
Object design model before transformation
* {ordered} *
Tournament Player
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16
Transformation of an association class
Object design model before transformation
Statistics
+getAverageStat(name)
+getTotalStat(name)
+updateStats(match)
Tournament Player
* *
Statistics
+getAverageStat(name)
+getTotalStat(name)
+updateStats(match)
1 1
Tournament Player
* *
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17
Other Mapping Activities
Optimizing the Object Design Model
Mapping Associations
Mapping Contracts to Exceptions Self reading
Mapping Object Models to Tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18
Mapping an object model to a relational database
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19
Mapping the User class to a database table
User
+firstName:String
+login:String
+email:String
User table
id:long firstName:text[25] login:text[8] email:text[32]
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20
Primary and Foreign Keys
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21
Example for Primary and Foreign Keys
Primary key
User table
1-1 mapping?
Bernd Bruegge & Allen H. Dutoit What about
Object-Oriented the 1
Software Engineering: Using (*) side?
UML, Patterns, and Java 23
Another Example for Buried Association
Portfolio
Transaction *
portfolioID
transactionID ...
Foreign Key
1-1 mapping?
Bernd Bruegge & Allen H. Dutoit What about
Object-Oriented the 1
Software Engineering: Using (*) side?
UML, Patterns, and Java 24
Mapping Many-To-Many Associations
In this case we need a separate table for the association
Primary Key
City Table Airport Table Serves Table
Tournament * * Player
to different tables
By duplicating columns (”horizontal mapping”) /* flattening
There is no table for the superclass
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27
Realizing inheritance with a separate table
(”vertical mapping”)
User
name
LeagueOwner Player
maxNumLeagues credits
User table
id name ... role
56 zoe LeagueOwner
79 john Player
LeagueOwner table Player table
id maxNumLeagues ... id credits ...
56 12 79 126
What should
Bernd Bruegge & Allen H. Dutoit be Software
Object-Oriented done toUsingretrieve
Engineering: UML, Patterns, and Java player info?
28
Realizing inheritance by duplicating columns
(”horizontal mapping”)
User
name
LeagueOwner Player
maxNumLeagues credits
What should
Bernd Bruegge be done when
& Allen H. Dutoit the
Object-Oriented Software“name” ofPatterns,
Engineering: Using UML, a and user
Java changes? 29
Comparison: Separate Tables vs Duplicated Columns
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30
Heuristics for Transformations
For a given transformation use the same tool
If you are using a CASE tool to map associations to code, use the tool
to change association multiplicities.
Keep the contracts in the source code, not in the object design
model
By keeping the specification as a source code comment, they are more
likely to be updated when the source code changes.
Use the same names for the same objects
If the name is changed in the model, change the name in the code and
or in the database schema.
Provides traceability among the models
Have a style guide for transformations
By making transformations explicit in a manual , all developers
can apply the transformation in the same way.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31
Summary
Undisciplined changes => degradation of the system model
Four mapping concepts were introduced
Model transformation improves the compliance of the object design model with
a design goal
Forward engineering improves the consistency of the code with respect to the
object design model
Refactoring improves the readability or modifiability of the code
Reverse engineering attempts to discover the design from the code.
We reviewed model transformation and forward engineering
techniques:
Optiziming the class model
Mapping associations to collections
Mapping contracts to exceptions
Mapping class model to storage schemas
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32