Figure 10 1 Model transformations refactorings forward engineering

  • Slides: 31
Download presentation
Figure 10 -1, Model transformations, refactorings, forward engineering, and reverse engineering. Forward engineering Refactoring

Figure 10 -1, Model transformations, refactorings, forward engineering, and reverse engineering. Forward engineering Refactoring Model transformation Reverse engineering Model space Bernd Bruegge & Allen H. Dutoit Source code space Object-Oriented Software Engineering: Using UML, Patterns, and Java 1

Figure 10 -2, An example of an object model transformation. Object design model before

Figure 10 -2, An example of an object model transformation. Object design model before transformation League. Owner +email: Address Advertiser +email: Address Player +email: Address Object design model after transformation User +email: Address League. Owner Bernd Bruegge & Allen H. Dutoit Advertiser Object-Oriented Software Engineering: Using UML, Patterns, and Java Player 2

Figure 10 -3, Applying the Pull Up Field refactoring. Before refactoring After refactoring public

Figure 10 -3, Applying the Pull Up Field refactoring. Before refactoring After refactoring public class Player { private String email; //. . . } public class League. Owner { private String e. Mail; //. . . } 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 League. Owner extends User { //. . . } public class Advertiser extends User { //. . . } Object-Oriented Software Engineering: Using UML, Patterns, and Java 3

Figure 10 -4, Pull Up Constructor Body refactoring. Before refactoring After refactoring public class

Figure 10 -4, Pull Up Constructor Body refactoring. Before refactoring After refactoring public class User { private String email; } public class User { public User(String email) { this. email = email; } } public class Player extends User { public Player(String email) { super(email); } } public class League. Owner extends User { public League. Owner(String email) { super(email); } } public class Advertiser extends User { public Advertiser(String email) { super(email); } } public class Player extends User { public Player(String email) { this. email = email; } } public class League. Owner extends User{ public League. Owner(String email) { this. email = email; } } public class Advertiser extends User{ public Advertiser(String email) { this. email = email; } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4

Figure 10 -5, Realization of User and League. Owner Object design model before transformation

Figure 10 -5, Realization of User and League. Owner Object design model before transformation User League. Owner +email: String +notify(msg: String) +max. Num. Leagues: int Source code after transformation public class User { private String email; public String get. Email() { return email; } public void set. Email(String value){ email = value; } public void notify(String msg) { //. . } /* Other methods omitted */ } Bernd Bruegge & Allen H. Dutoit public class League. Owner extends User { private int max. Num. Leagues; public int get. Max. Num. Leagues() { return max. Num. Leagues; } public void set. Max. Num. Leagues (int value) { max. Num. Leagues = value; } /* Other methods omitted */ } Object-Oriented Software Engineering: Using UML, Patterns, and Java 5

Figure 10 -6, Collapsing an object without interesting behavior into an attribute Object design

Figure 10 -6, Collapsing an object without interesting behavior into an attribute Object design model before transformation Person Social. Security number: String Object design model after transformation Person SSN: String Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6

Figure 10 -7, Delaying expensive computations Object design model before transformation Image filename: String

Figure 10 -7, Delaying expensive computations Object design model before transformation Image filename: String data: byte[] paint() Object design model after transformation Image filename: String paint() Image. Proxy filename: String paint() Bernd Bruegge & Allen H. Dutoit image 1 0. . 1 Real. Image data: byte[] paint() Object-Oriented Software Engineering: Using UML, Patterns, and Java 7

Figure 10 -8, Realization of a unidirectional, one-toone association Object design model before transformation

Figure 10 -8, Realization of a unidirectional, one-toone 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 get. Account() { return account; } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8

Figure 10 -9, Realization of a bidirectional one-to-one association Object design model before transformation

Figure 10 -9, Realization of a bidirectional one-to-one association Object design model before transformation 1 Advertiser 1 Account Source code after transformation public class Advertiser extends User{ /* The account field is initialized * in the constructor and never * modified. */ private Account account; public Advertiser() { account = new Account(this); } public Account get. Account() { return account; } public class Account { /* The owner field is initialized * during the constructor and * never modified. */ private Advertiser owner; public Account(owner: Advertiser) { this. owner = owner; } public Advertiser get. Owner() { return owner; } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9

Figure 10 -10, Realization of a bidirectional, one-tomany association Object design model before transformation

Figure 10 -10, Realization of a bidirectional, one-tomany association Object design model before transformation Advertiser 1 * Account Source code after transformation public class Advertiser { private Set accounts; public Advertiser() { accounts = new Hash. Set(); } public void add. Account(Account a) { accounts. add(a); a. set. Owner(this); } public void remove. Account(Account a) { accounts. remove(a); a. set. Owner(null); } } Bernd Bruegge & Allen H. Dutoit public class Account { private Advertiser owner; public void set. Owner(Advertiser new. Owner) { if (owner != new. Owner) { Advertiser old = owner; owner = new. Owner; if (new. Owner != null) new. Owner. add. Account(this); if (old. Owner != null) old. remove. Account(this); } } } Object-Oriented Software Engineering: Using UML, Patterns, and Java 10

Figure 10 -11, Realization of a bidirectional, many-tomany association Object design model before transformation

Figure 10 -11, Realization of a bidirectional, many-tomany association Object design model before transformation Tournament * {ordered} * Player Source code after transformation public class Tournament { private List players; public Tournament() { players = new Array. List(); } public void add. Player(Player p) { if (!players. contains(p)) { players. add(p); p. add. Tournament(this); } } } Bernd Bruegge & Allen H. Dutoit public class Player { private List tournaments; public Player() { tournaments = new Array. List(); } public void add. Tournament(Tournament t) { if (!tournaments. contains(t)) { tournaments. add(t); t. add. Player(this); } } } Object-Oriented Software Engineering: Using UML, Patterns, and Java 11

Figure 10 -12, Realization of a bidirectional qualified association Object design model before transformation

Figure 10 -12, Realization of a bidirectional qualified association Object design model before transformation League * * Player Object design model before forward engineering League Bernd Bruegge & Allen H. Dutoit nick. Name * 0. . 1 Object-Oriented Software Engineering: Using UML, Patterns, and Java Player 12

Figure 10 -12, Realization of a bidirectional qualified association (continued) Source code after forward

Figure 10 -12, Realization of a bidirectional qualified association (continued) Source code after forward engineering public class League { private Map players; } public class Player { private Map leagues; public void add. Player (String nick. Name, Player p) { if (!players. contains. Key(nick. Name)) { players. put(nick. Name, p); p. add. League(nick. Name, this); } } } Bernd Bruegge & Allen H. Dutoit public void add. League (String nick. Name, League l) { if (!leagues. contains. Key(l)) { leagues. put(l, nick. Name); l. add. Player(nick. Name, this); } } Object-Oriented Software Engineering: Using UML, Patterns, and Java 13

Figure 10 -13, Transformation of an association class into an object and two binary

Figure 10 -13, Transformation of an association class into an object and two binary associations Object design model before transformation Statistics +get. Average. Stat(name) +get. Total. Stat(name) +update. Stats(match) Tournament * * Player Object design model after transformation Statistics +get. Average. Stat(name) +get. Total. Stat(name) +update. Stats(match) 1 Tournament Bernd Bruegge & Allen H. Dutoit * 1 * Object-Oriented Software Engineering: Using UML, Patterns, and Java Player 14

Figure 10 -14, Example of exception handling in Java. public class Tournament. Control {

Figure 10 -14, Example of exception handling in Java. public class Tournament. Control { private Tournament tournament; public void add. Player(Player p) throws Known. Player. Exception { if (tournament. is. Player. Accepted(p)) { throw new Known. Player. Exception(p); } //. . . Normal add. Player behavior } } public class Tournament. Form { private Tournament. Control control; private Array. List players; public void process. Player. Applications() { // Go through all the players who applied for this tournament for (Iteration i = players. iterator(); i. has. Next(); ) { try { // Delegate to the control object. control. accept. Player((Player)i. next()); } catch (Known. Player. Exception e) { // If an exception was caught, log it to the console, and // proceed to the next player. Error. Console. log(e. get. Message()); } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15

Figure 10 -15, A complete implementation of the Tournament. add. Player() contract. «invariant» get.

Figure 10 -15, A complete implementation of the Tournament. add. Player() contract. «invariant» get. Max. Num. Players() > 0 Tournament «precondition» !is. Player. Accepted(p) -max. Num. Players: int +get. Num. Players(): int +get. Max. Num. Players(): int +is. Player. Accepted(p: Player): boolean +add. Player(p: Player) «precondition» get. Num. Players() < get. Max. Num. Players() Bernd Bruegge & Allen H. Dutoit «postcondition» is. Player. Accepted(p) Object-Oriented Software Engineering: Using UML, Patterns, and Java 16

Figure 10 -16, An example of a relational table, with three attributes and three

Figure 10 -16, An example of a relational table, with three attributes and three data records. Primary key User table first. Name login email “alice” “am 384@mail. org” “john” “js 289” “john@mail. de” “bob” “bd” “bobd@mail. ch” Candidate key Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java Candidate key 17

Figure 10 -17, An example of a foreign key. The owner attribute in the

Figure 10 -17, An example of a foreign key. The owner attribute in the League table refers to the primary key of the User table in Figure 10 -16. League table name login “tictactoe. Novice” “am 384” “tictactoe. Expert” “am 384” “chess. Novice” “js 289” Foreign key referencing User table Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18

Figure 10 -18, Forward engineering of the User class to a database table User

Figure 10 -18, Forward engineering of the User class to a database table User +first. Name: String +login: String +email: String User table id: long first. Name: text[25] Bernd Bruegge & Allen H. Dutoit login: text[8] Object-Oriented Software Engineering: Using UML, Patterns, and Java email: text[32] 19

Figure 10 -19, Mapping of the League. Owner/League association as a buried association. League.

Figure 10 -19, Mapping of the League. Owner/League association as a buried association. League. Owner 1 * League. Owner table id: long Bernd Bruegge & Allen H. Dutoit . . . League table id: long . . . Object-Oriented Software Engineering: Using UML, Patterns, and Java owner: long 20

Figure 10 -20, Mapping of the Tournament/Player association as a separate table. Tournament table

Figure 10 -20, Mapping of the Tournament/Player association as a separate table. Tournament table id name 23 24 * Player Tournament. Player. Association table Player table tournament player id name novice 23 56 56 alice expert 23 79 79 john Bernd Bruegge & Allen H. Dutoit . . . * Object-Oriented Software Engineering: Using UML, Patterns, and Java . . . 21

Figure 10 -21, Realizing the User inheritance hierarchy with a separate table. User name

Figure 10 -21, Realizing the User inheritance hierarchy with a separate table. User name League. Owner Player max. Num. Leagues credits User table id name 56 zoe 79 john . . . role League. Owner Player League. Owner table id 56 max. Num. Leagues 12 Bernd Bruegge & Allen H. Dutoit Player table. . . id credits 79 126 Object-Oriented Software Engineering: Using UML, Patterns, and Java . . . 22

Figure 10 -22, Realizing the User inheritance hierarchy by duplicating columns. User name League.

Figure 10 -22, Realizing the User inheritance hierarchy by duplicating columns. User name League. Owner Player max. Num. Leagues credits League. Owner table id name 56 zoe Bernd Bruegge & Allen H. Dutoit max. Num. Leagues 12 Player table. . . id name credits 79 john 126 Object-Oriented Software Engineering: Using UML, Patterns, and Java . . . 23

Figure 10 -23, Statistics as a product in the Game Abstract Factory Game Tournament

Figure 10 -23, Statistics as a product in the Game Abstract Factory Game Tournament create. Statistics() Tic. Tac. Toe. Game Chess. Game Statistics update() get. Stat() TTTStatistics Bernd Bruegge & Allen H. Dutoit Chess. Statistics Object-Oriented Software Engineering: Using UML, Patterns, and Java Default. Statistics 24

Figure 10 -24, N-ary association class Statistics relating League, Tournament, and Player Statistics 1

Figure 10 -24, N-ary association class Statistics relating League, Tournament, and Player Statistics 1 * 1 0. . 1 Game Bernd Bruegge & Allen H. Dutoit 0. . 1 1 0. . 1 League Tournament Object-Oriented Software Engineering: Using UML, Patterns, and Java 0. . 1 Player 25

Figure 10 -25, Simple. Statistics. Vault object realizing the N-ary association of Figure 10

Figure 10 -25, Simple. Statistics. Vault object realizing the N-ary association of Figure 10 -24. Tournament. Control Statistics. View Simple. Statistics. Vault Statistics get. Statistics. Object(game, player) get. Statistics. Object(league, player) get. Statistics. Object(tournament, player) update(match, player) get. Stat. Names() get. Stat(name) Game create. Statistics() Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 26

Figure 10 -26, Statistics. Vault as a Facade shielding the control and boundary objects

Figure 10 -26, Statistics. Vault as a Facade shielding the control and boundary objects from the Statistics storage and computation Tournament. Control Statistics. View Statistics. Vault Statistics update(match) get. Stat. Names(game) get. Stat(name, game, player) get. Stat(name, league, player) get. Stat(name, tournament, player) update(match, player) get. Stat. Names() get. Stat(name) Game create. Statistics() Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27

Figure 10 -27, Public interface of the Statistics. Vault class (Java). public class Statistics.

Figure 10 -27, Public interface of the Statistics. Vault class (Java). public class Statistics. Vault { public void update(Match m) throws Invalid. Match, Match. Not. Completed {. . . } public List get. Stat. Names() {. . . } public double get. Stat(String name, Game g, Player p) throws Unknown. Statistic, Invalid. Scope {. . . } public double get. Stat(String name, League l, Player p) throws Unknown. Statistic, Invalid. Scope {. . . } public double get. Stat(String name, Tournament t, Player p) throws Unknown. Statistic, Invalid. Scope {. . . } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28

Figure 10 -28, Database schema for the Statistics Nary association of Figure 10 -24.

Figure 10 -28, Database schema for the Statistics Nary association of Figure 10 -24. Statistics table id: long scopetype: long player: long Statistic. Counters table id: long name: text[25] value: double Game table id: long League table. . . Bernd Bruegge & Allen H. Dutoit id: long Tournament table. . . Object-Oriented Software Engineering: Using UML, Patterns, and Java id: long . . . 29

Figure 10 -29, Associations among Messages, Folders, Mailboxes, and Views in a hypothetical email

Figure 10 -29, Associations among Messages, Folders, Mailboxes, and Views in a hypothetical email client Mailbox Folder 1 * Message 1 * * * View Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30

Figure 10 -30, Associations among League, Tournament, Round, and Player within ARENA League Tournament

Figure 10 -30, Associations among League, Tournament, Round, and Player within ARENA League Tournament 1 * * Round 1 * * Player Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31