Art for Chapter 10, Mapping Models to Code

Download Report

Transcript Art for Chapter 10, Mapping Models to Code

Using UML, Patterns, and Java
Object-Oriented Software Engineering
Art for Chapter 10,
Mapping Models to Code
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
2
Figure 10-2, An example of an object model
transformation.
Object design model before transformation
LeagueOwner
+email:Address
Advertiser
+email:Address
Player
+email:Address
Object design model after transformation
User
+email:Address
LeagueOwner
Bernd Bruegge & Allen H. Dutoit
Advertiser
Object-Oriented Software Engineering: Using UML, Patterns, and Java
Player
3
Figure 10-3, Applying the Pull Up Field refactoring.
Before refactoring
After refactoring
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
4
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 LeagueOwner extends
User {
public LeagueOwner(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 LeagueOwner extends
User{
public LeagueOwner(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
5
Figure 10-5, Realization of User and LeagueOwner
Object design model before transformation
User
LeagueOwner
+email:String
+notify(msg:String)
+maxNumLeagues:int
Source code after transformation
public class User {
private String email;
public String getEmail() {
return email;
}
public void setEmail(String value){
email = value;
}
public void notify(String msg) {
// ....
}
/* Other methods omitted */
}
Bernd Bruegge & Allen H. Dutoit
public class LeagueOwner extends User {
private int maxNumLeagues;
public int getMaxNumLeagues() {
return maxNumLeagues;
}
public void setMaxNumLeagues
(int value) {
maxNumLeagues = value;
}
/* Other methods omitted */
}
Object-Oriented Software Engineering: Using UML, Patterns, and Java
6
Figure 10-6, Collapsing an object without interesting
behavior into an attribute
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
7
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()
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
8
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 getAccount() {
return account;
}
}
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
9
Figure 10-9, Realization of a bidirectional one-to-one
association
Object design model before transformation
Advertiser
1
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 getAccount() {
return account;
}
}
Bernd Bruegge & Allen H. Dutoit
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 getOwner() {
return owner;
}
}
Object-Oriented Software Engineering: Using UML, Patterns, and Java
10
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 HashSet();
}
public void addAccount(Account a) {
accounts.add(a);
a.setOwner(this);
}
public void removeAccount(Account a) {
accounts.remove(a);
a.setOwner(null);
}
}
Bernd Bruegge & Allen H. Dutoit
public class Account {
private Advertiser owner;
public void setOwner(Advertiser newOwner) {
if (owner != newOwner) {
Advertiser old = owner;
owner = newOwner;
if (newOwner != null)
newOwner.addAccount(this);
if (oldOwner != null)
old.removeAccount(this);
}
}
}
Object-Oriented Software Engineering: Using UML, Patterns, and Java
11
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 {
public class Player {
private List players;
private List tournaments;
public Tournament() {
public Player() {
players = new ArrayList();
tournaments = new ArrayList();
}
}
public void addPlayer(Player p) {
public void addTournament(Tournament t)
{
if (!players.contains(p)) {
if (!tournaments.contains(t)) {
players.add(p);
tournaments.add(t);
p.addTournament(this);
t.addPlayer(this);
}
}
}
}
}
}
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
12
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
nickName
*
0..1
Object-Oriented Software Engineering: Using UML, Patterns, and Java
Player
13
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 addPlayer
(String nickName, Player p) {
if (!players.containsKey(nickName))
{
players.put(nickName, p);
p.addLeague(nickName, this);
}
}
}
Bernd Bruegge & Allen H. Dutoit
public void addLeague
(String nickName, League l) {
if (!leagues.containsKey(l)) {
leagues.put(l, nickName);
l.addPlayer(nickName, this);
}
}
Object-Oriented Software Engineering: Using UML, Patterns, and Java
14
Figure 10-13, Transformation of an association class
into an object and two binary associations
Object design model before transformation
Statistics
+getAverageStat(name)
+getTotalStat(name)
+updateStats(match)
Tournament
*
*
Player
Object design model after transformation
Statistics
+getAverageStat(name)
+getTotalStat(name)
+updateStats(match)
1
Tournament
Bernd Bruegge & Allen H. Dutoit
*
1
*
Object-Oriented Software Engineering: Using UML, Patterns, and Java
Player
15
Figure 10-14, Example of exception handling 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 who applied for this tournament
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, and
// proceed to the next player.
ErrorConsole.log(e.getMessage());
}
}
}
}
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
16
Figure 10-15, 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
17
Figure 10-16, An example of a relational table, with
three attributes and three data records.
Primary key
User table
firstName
login
email
“alice”
“am384”
“[email protected]”
“john”
“js289”
“[email protected]”
“bob”
“bd”
“[email protected]”
Candidate key
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
Candidate key
18
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
“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
19
Figure 10-18, Forward engineering of 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]
20
Figure 10-19, Mapping of the LeagueOwner/League
association as a buried association.
LeagueOwner
1
*
LeagueOwner table
id:long
Bernd Bruegge & Allen H. Dutoit
...
League
League table
id:long
...
Object-Oriented Software Engineering: Using UML, Patterns, and Java
owner:long
21
Figure 10-20, Mapping of the Tournament/Player
association as a separate table.
Tournament
Tournament table
id
name
23
24
*
Player
TournamentPlayerAssociation 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
...
22
Figure 10-21, Realizing the User inheritance
hierarchy with a separate table.
User
name
LeagueOwner
Player
maxNumLeagues
credits
User table
id
name
56
zoe
79
john
...
role
LeagueOwner
Player
LeagueOwner table
id
56
maxNumLeagues
12
Bernd Bruegge & Allen H. Dutoit
Player table
...
id
credits
79
126
Object-Oriented Software Engineering: Using UML, Patterns, and Java
...
23
Figure 10-22, Realizing the User inheritance
hierarchy by duplicating columns.
User
name
LeagueOwner
Player
maxNumLeagues
credits
Player table
LeagueOwner table
id
name
56
zoe
Bernd Bruegge & Allen H. Dutoit
maxNumLeagues
12
...
id
name
credits
79
john
126
Object-Oriented Software Engineering: Using UML, Patterns, and Java
...
24
Figure 10-23, Statistics as a product in the Game
Abstract Factory
Game
Tournament
createStatistics()
TicTacToeGame
ChessGame
Statistics
update()
getStat()
TTTStatistics
Bernd Bruegge & Allen H. Dutoit
ChessStatistics
Object-Oriented Software Engineering: Using UML, Patterns, and Java
DefaultStatistics
25
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
26
Figure 10-25, SimpleStatisticsVault object realizing
the N-ary association of Figure 10-24.
TournamentControl
StatisticsView
SimpleStatisticsVault
getStatisticsObject(game,player)
getStatisticsObject(league,player)
getStatisticsObject(tournament,player)
Statistics
update(match,player)
getStatNames()
getStat(name)
Game
createStatistics()
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
27
Figure 10-26, StatisticsVault as a Facade shielding
the control and boundary objects from the Statistics
storage and computation
TournamentControl
StatisticsView
StatisticsVault
update(match)
getStatNames(game)
getStat(name,game,player)
getStat(name,league,player)
getStat(name,tournament,player)
Statistics
update(match,player)
getStatNames()
getStat(name)
Game
createStatistics()
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
28
Figure 10-27, Public interface of the StatisticsVault
class (Java).
public class StatisticsVault {
public void update(Match m)
throws InvalidMatch, MatchNotCompleted {...}
public List getStatNames() {...}
public double getStat(String name, Game g, Player p)
throws UnknownStatistic, InvalidScope {...}
public double getStat(String name, League l, Player p)
throws UnknownStatistic, InvalidScope {...}
public double getStat(String name, Tournament t, Player p)
throws UnknownStatistic, InvalidScope {...}
}
Bernd Bruegge & Allen H. Dutoit
Object-Oriented Software Engineering: Using UML, Patterns, and Java
29
Figure 10-28, Database schema for the Statistics Nary association of Figure 10-24.
Statistics table
id:long
scope:long
scopetype:long
player:long
StatisticCounters table
id:long
name:text[25]
League table
Game table
id:long
value:double
...
Bernd Bruegge & Allen H. Dutoit
id:long
Tournament table
...
Object-Oriented Software Engineering: Using UML, Patterns, and Java
id:long
...
30
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
31
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
32