Tut 07
Tut 07
Software Engineering
and Project Management
Presentation Outlines
Review of last tutorial
OCL – Object Constraint Language
Unit5:
Implementation and Testing
Q&A
Overview of mapping
Mapping concepts
Source Target Type of transformation
User
+email:Address
public class Player extends User { public class Player extends User {
public Player(String email) { public Player(String email) {
this.email = email; super(email);
} }
} }
public class LeagueOwner extends User { public class LeagueOwner extends User {
public LeagueOwner(String email) { public LeagueOwner(String email) {
this.email = email; super(email);
} }
} }
public class Advertiser extends User { public class Advertiser extends User {
public Advertiser(String email) { public Advertiser(String email) {
this.email = email; super(email);
} }
} }
Pull Up Method
Before refactoring After refactoring
public class User { public class User {
private String email; private String email;
} public String getEmail( ) {
return email;
}
}
public class Player extends User { public class Player extends User {
public String getEmail( ) { …
return email; }
}
} public class LeagueOwner extends User {
…
public class LeagueOwner extends User { }
public String getEmail( ) {
return email; public class Advertiser extends User {
} …
} }
User LeagueOwner
+email:String +maxNumLeagues:int
+notify(msg:String)
Object design model before transformation
User LeagueOwner
+email:String +maxNumLeagues:int
+notify(msg:String)
Software tools can’t generate models accurately from
program code automatically.
Difference between reverse engineering and
reengineering
Transformation principles
Prevent multiple transformations at the
same time.
Retest the program after refactoring.
Mapping Activities
Mapping Activities
Optimizing the object design model
Optimizing associations
• For quick access to an object
matches
Match
*
start
status
playMove()
getScore() Traversal from Match to Advertiser could
be more efficient by direct associations
Optimizing the object model
Optimizing access paths
Optimization of a ‘many’ association to a ‘one’
qualified association
Without qualification
1 * File
Directory
filename
With qualification
1 0..1
Directory filename File
How ? HashTable
Concept of Hash table
Reference: Wikipedia.org
Optimizing the object model
Optimizing access paths
Look for attributes that are misplaced in the wrong
objects.
* *
Course Student
courseID : int studentID: int
name: String name: String
age : int
maxSize: int
How to fix the diagram above?
Optimizing the object model
Collapsing objects: turning objects into attributes
Object design model before transformation
Person ID
number:String
Person
ID:String
Optimizing the object model
Delaying expensive computations
Caching the result of computations
Reference: Wikipedia.org
Mapping association to collections
Unidirectional one-to-one associations
1 1
class A class B
Class A { Class B {
B partner; // …
// … }
}
Object design model before transformation
1 1
Advertiser Account
public Advertiser() {
account = new Account();
}
1 1
class A class B
Class A { Class B {
B partner; A partner;
// … // …
} }
Object design model before transformation
1 1
Advertiser Account
1 *
class A class B
Class A { Class B {
HashSet bs; // …
// … }
}
Object design model before transformation (uni-directional)
Advertiser 1 * Account
while (iterator.hasNext( ))
{
Account = (Account)iterator.next();
...
}
Mapping association to collections
Many-to-many associations
Using List (ArrayList, LinkedList and Vector) provided by the
java.util package
* *
class A class B
Class A { Class B {
List bList; List aList;
// … // …
} }
Object design model before transformation
* *
Tournament Player
public KnownPlayerException ( ) {
super( “Attempted to add a player already found in a
tournament” );
}
}
Mapping contracts to exceptions
Throwing an exception
Declaration
Raise the exception
throw new KnownPlayerException (p, tournament);
Mapping contracts to exceptions
Catching an exception
Try-catch block
try {
...
control.addPlayer( p );
...
} catch (KnownPlayerException e) {
ErrorConsole.log( e.getMessage( ) );
}
When exception occurs in try block, the
program will flow to the catch block directly.
public class TournamentControl {
private Tournament ;
public void addPlayer(Player p) throws KnownPlayerException {
if (tournament.isPlayerAccepted(p)) {
throw new KnownPlayerException(p, tournament);
}
//... Normal addPlayer behavior
}
}
User
+firstName:String
+login:String
+email:String
User table
Primary key
User table
bob bd [email protected]
Game table
Foreign key
referencing
User table
(login field)
Mapping object models to persistent storage schema
Mapping associations
Mapping 1-to-1 or 1-to-many associations with buried
association
1 *
GameOwner Game
Mapping associations
Mapping many-to-many associations with new
association table
* *
Tournament Player
Vertical mapping
• One table for each superclass and subclass
Horizontal mapping
• One table for every concrete class
Vertical mapping
User
id
name
GameOwner Player
maxNumGames credits
User table
id name role
56 Zoe GameOwner
79 John Player
56 12 79 126
Horizontal mapping
User
id
name
GameOwner Player
maxNumGames credits
3.pressButtonsLAndR
1.
2. 6.
pressButtonL pressButtonL
pressButtonR MeasureTime pressButtonR
SetTime
4.after 2 min.
5.pressButtonsLAndR/beep
Layer 2
Layer 3