Using UML, Patterns, and Java Object-Oriented Software Engineering Mapping Models to Code Partially based on Linda Sherrell’slides Overview ♦ ♦ Object design is situated between system design and implementation. Needs to understand well before implementation. A selection of transformations to illustrate a disciplined approach to avoid system degradation. 1. Operations on the object model: t Optimizations to address performance requirements 2. Implementation of class model components: t Realization of associations t Realization of operation contracts 3. Realizing entity objects based on selected storage strategy t Mapping the class model to a storage schema Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2 State of the Art of Model-based Software Engineering ♦ The Vision w During object design we would like to implement a system that realizes the use cases specified during requirements elicitation and system design. ♦ The Reality w Different developers usually handle contract violations differently. w Undocumented parameters are often added to the API to address a requirement change. w Additional attributes are usually added to the object model, but are not handled by the persistent data management system, possibly because of a miscommunication. w Many improvised code changes and workarounds that eventually yield to the degradation of the system. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3 Model Transformation Example Object design model before transformation LeagueOwner +email:Address Object design model after transformation: Player Advertiser +email:Address +email:Address User +email:Address LeagueOwner Bernd Bruegge & Allen H. Dutoit Advertiser Object-Oriented Software Engineering: Using UML, Patterns, and Java Player 4 Refactoring Example: Pull Up Field to SuperClass public class Player { private String email; //... } public class LeagueOwner { private String eMail; //... } public class Advertiser { private String email_address; //... } Bernd Bruegge & Allen H. Dutoit public class User { private String email; } public class Player extends User { //... } public class LeagueOwner extends User { //... } public class Advertiser extends User { //... } Object-Oriented Software Engineering: Using UML, Patterns, and Java 5 Refactoring Example: Pull Up Constructor Body public class User { private String email; } public class Player extends User { public Player(String email) { this.email = email; } } public class LeagueOwner extends User{ } public LeagueOwner(String email) { this.email = email; } public Advertiser(String email) { this.email = email; } Bernd Bruegge & Allen H. Dutoit public class LeagueOwner extends User { } public LeagueOwner(String email) { super(email); } public class Advertiser extends User { public class Advertiser extendsUser{ } public class User { public User(String email) { this.email = email; } } public class Player extends User { public Player(String email) { super(email); } } } public Advertiser(String email) { super(email); } Object-Oriented Software Engineering: Using UML, Patterns, and Java 6 Other Mapping Activities ♦ Optimizing the Object Design Mode: Minimize response time, execution time or memory use w Collapsing objects into attributes w Delaying expensive computations w Caching the results of expensive computations w Adding associations to optimize access paths 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 7 Collapsing objects into attributes Object design model before transformation Person SocialSecurity number:String Object design model after transformation Person SSN:String Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8 Delaying expensive computations Object design model before transformation Image filename:String data:byte[] paint() Object design model after transformation Image filename:String paint() ImageProxy filename:String paint() Bernd Bruegge & Allen H. Dutoit image 1 0..1 RealImage data:byte[] paint() Object-Oriented Software Engineering: Using UML, Patterns, and Java 9 Other Mapping Activities Optimizing the Object Design Model Ø Mapping Associations ü Ø Unidirectional one-to-one associations Ø Bidirectional one-to-one associations Ø One-to-many associations Ø Many-to-many associations Mapping Contracts to Exceptions ♦ Mapping Object Models to Tables ♦ Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10 Realization of a unidirectional, one-to-one association Object design model before transformation Advertiser 1 1 Account ? 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 11 Bidirectional one-to-one association Object design model before transformation Advertiser 1 1 Account Source code after transformation public class Advertiser { /* The account field is initialized * in the constructor and never * modified. */ private Account account; public class Account { /* The owner field is initialized * during the constructor and * never modified. */ private Advertiser owner; public Account(owner:Advertiser) { public Advertiser() { this.owner = owner; account = new Account(this); } public Advertiser getOwner() } { public Account getAccount() { return owner; return account; } Object-Oriented Software Engineering: Using UML, Patterns, and Java 12 }Bernd Bruegge & Allen H. Dutoit } Bidirectional, one-to-many association Object design model before transformation Advertiser 1 * Account Source code after transformation public class Advertiser { public class Account { private Set accounts; private Advertiser owner; public Advertiser() { public void setOwner(Advertiser accounts = new newOwner) { HashSet(); if (owner != newOwner) { } Advertiser old = owner; public void owner = newOwner; addAccount(Account a) { if (newOwner != null) accounts.add(a); newOwner.addAccount(this); a.setOwner(this); if (oldOwner != null) } old.removeAccount(this); public void } removeAccount(Account a) { } accounts.remove(a); } a.setOwner(null); }Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13 Bidirectional, many-to-many association Object design model before transformation Tournament * {ordered} * Player Source code after transformation public class Player { private List tournaments; public Player() { tournaments = new ArrayList(); } public void addTournament (Tournament t) { if (!tournaments.contains(t)) { tournaments.add(t); t.addPlayer(this); } } Object-Oriented Software Engineering: Using UML, Patterns, and Java 14 public class Tournament { private List players; public Tournament() { players = new ArrayList(); } public void addPlayer(Player p) { if (!players.contains(p)) { players.add(p); p.addTournament(this); } } } Bernd Bruegge & Allen H. Dutoit 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 15 Exceptions as building blocks for contract violations Many object-oriented languages, including Java do not include built-in support for contracts. ♦ However, we can use their exception mechanisms as building blocks for signaling and handling contract violations ♦ In Java we use the try-throw-catch mechanism ♦ Example: ♦ w Let us assume the acceptPlayer() operation of TournamentControl is invoked with a player who is already part of the Tournament. w In this case acceptPlayer() should throw an exception of type KnownPlayer. w See source code on next slide Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16 The try-throw-catch Mechanism in Java public class TournamentControl { private Tournament tournament; public void addPlayer(Player p) throws KnownPlayerException { if (tournament.isPlayerAccepted(p)) { throw new KnownPlayerException(p); } //... Normal addPlayer behavior } } public class TournamentForm { private TournamentControl control; private ArrayList players; public void processPlayerApplications() { // Go through all the // players for (Iteration i = players.iterator(); i.hasNext();) { try { // Delegate to the control object. control.acceptPlayer((Player)i.next()); } catch (KnownPlayerException e) { // If an exception was caught, log it to the // console ErrorConsole.log(e.getMessage()); } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17 Implementing a contract For each operation in the contract, do the following ♦ Check precondition: Check the precondition before the beginning of the method with a test that raises an exception if the precondition is false. ♦ Check postcondition: Check the postcondition at the end of the method and raise an exception if the contract is violoated. If more than one postcondition is not satisfied, raise an exception only for the first violation. ♦ Check invariant: Check invariants at the same time as postconditions. ♦ Deal with inheritance: Encapsulate the checking code for preconditions and postconditions into separate methods that can be called from subclasses. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18 A complete implementation of the Tournament.addPlayer() contract «invariant» getMaxNumPlayers() > 0 Tournament «precondition» !isPlayerAccepted(p) -maxNumPlayers: int +getNumPlayers():int +getMaxNumPlayers():int +isPlayerAccepted(p:Player):boolean +addPlayer(p:Player) «precondition» getNumPlayers() < getMaxNumPlayers() Bernd Bruegge & Allen H. Dutoit «postcondition» isPlayerAccepted(p) Object-Oriented Software Engineering: Using UML, Patterns, and Java 19 Heuristics for Mapping Contracts to Exceptions Be pragmatic, if you don’t have enough time. ♦ Omit checking code for postconditions and invariants. ♦ Omit the checking code for private and protected methods. ♦ Focus on components with the longest life w Focus on Entity objects, not on boundary objects associated with the user interface. ♦ Reuse constraint checking code. w Many operations have similar preconditions. w Encapsulate constraint checking code into methods so that they can share the same exception classes. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20 Other Mapping Activities Optimizing the Object Design Model ü Mapping Associations ü Mapping Contracts to Exceptions Ø Mapping Object Models to Tables ü w Mapping classes to attributes w Mapping associations w Mapping inheritance relationships Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21 Mapping an object model to a relational database ♦ UML object models can be mapped to relational databases: w Some degradation occurs because all UML constructs must be mapped to a single relational database construct - the table. ♦ UML mappings w Each class is mapped to a table w Each class attribute is mapped onto a column in the table w An instance of a class represents a row in the table w A many-to-many association is mapped into its own table w A one-to-many association is implemented as buried foreign key ♦ Methods are not mapped Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 22 Mapping the User class to a database table User +firstName:String +login:String +email:String User table" id:long firstName:text[25] Bernd Bruegge & Allen H. Dutoit login:text[8] Object-Oriented Software Engineering: Using UML, Patterns, and Java email:text[32] 23 Primary and Foreign Keys ♦ The primary key of a table is a set of attributes whose values uniquely identify the data records in the table. ♦ A foreign key is an attribute (or a set of attributes) that references the primary key of another table. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24 Example for Primary and Foreign Keys Primary key User table fir stName login email “alice” “am384” “am384@mail.org” “john” “js289” “john@mail.de” “bob” “bd” “bobd@mail.ch” Candidate key League table name Candidate key login “tictactoeNovice” “am384” “tictactoeExpert” “am384” “chessNovice” “js289” Foreign key referencing User table Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 25 Buried Association Associations with multiplicity “one” can be implemented using a foreign key. Because the association vanishes in the table, we call this a buried association. ♦ For one-to-many associations we add the foreign key to the table representing the class on the “many” end. ♦ For all other associations we can select either class at the end of the association. ♦ LeagueOwner LeagueOwner table id:long ... Bernd Bruegge & Allen H. Dutoit 1 * League League table id:long ... Object-Oriented Software Engineering: Using UML, Patterns, and Java o wner:long 26 Mapping Many-To-Many Associations In this case we need a separate table for the association City * Serves * cityName Airport airportCode airportName Separate table for" “Serves” association" Primary Key" Airport Table City Table airportCode IAH HOU ALB MUC HAM cityName Houston Albany Munich Hamburg Bernd Bruegge & Allen H. Dutoit Serves Table airportName Intercontinental Hobby Albany County Munich Airport Hamburg Airport Object-Oriented Software Engineering: Using UML, Patterns, and Java cityName airportCode IAH Houston HOU Houston ALB Albany MUC Munich HAM Hamburg 27 Mapping the Tournament/Player association as a separate table Tournament * * Player Tournament table id name 23 novice 24 exper t Bernd Bruegge & Allen H. Dutoit Player table TournamentPlayerAssociation " table ... tournament player 23 56 23 79 Object-Oriented Software Engineering: Using UML, Patterns, and Java id name 56 alice 79 john 28 ... Realizing Inheritance Relational databases do not support inheritance ♦ Two possibilities to map UML inheritance relationships to a database schema ♦ w With a separate table (vertical mapping) t The attributes of the superclass and the subclasses are mapped to different tables w By duplicating columns (horizontal mapping) t There is no table for the superclass t Each subclass is mapped to a table containing the attributes of the subclass and the attributes of the superclass Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29 Realizing inheritance with a separate table User name Player credits LeagueOwner maxNumLeagues User table" id name 56 zoe 79 john ... role LeagueOwner Play er LeagueOwner table" id 56 maxNumLeagues ... 12 Bernd Bruegge & Allen H. Dutoit Player table" id credits ... 79 Object-Oriented Software Engineering: Using UML, Patterns, and Java 126 30 Realizing inheritance by duplicating columns User name LeagueOwner maxNumLeagues Player credits Player table LeagueOwner table" id name 56 zoe Bernd Bruegge & Allen H. Dutoit maxNumLeagues ... id name credits 12 79 john 126 Object-Oriented Software Engineering: Using UML, Patterns, and Java 31 ... Comparison: Separate Tables vs Duplicated Columns ♦ The trade-off is between modifiability and response time w How likely is a change of the superclass? w What are the performance requirements for queries? ♦ Separate table mapping J We can add attributes to the superclass easily by adding a column to the superclass table L Searching for the attributes of an object requires a join operation. ♦ Duplicated columns L Modifying the database schema is more complex and error-prone J Individual objects are not fragmented across a number of tables, resulting in faster queries Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32 Summary Undisciplined changes => degradation of the system model ♦ Four mapping concepts were introduced ♦ w Improves the compliance of the object design model with a design goal w Improves the consistency of the code with respect to the object design model w Improves the readability or modifiability of the code w Attempts to discover the design from the code. ♦ We reviewed model transformation and forward engineering techniques: w Optiziming the class model w Mapping associations to collections w Mapping contracts to exceptions w Mapping class model to storage schemas Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 33