art for chapter 10, mapping models to code

32
Using UML, Patterns, and Java Object-Oriented Software Engineering Art for Chapter 10, Mapping Models to Code

Upload: adele

Post on 14-Jan-2016

64 views

Category:

Documents


0 download

DESCRIPTION

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. Source code space. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Art for Chapter 10, Mapping Models to Code

Usi

ng U

ML

, Pat

tern

s, a

nd J

ava

Ob

ject

-Ori

ente

d S

oftw

are

En

gin

eeri

ng

Art for Chapter 10,Mapping Models to Code

Page 2: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2

Figure 10-1, Model transformations, refactorings, forward engineering, and reverse engineering.

Source code spaceModel space

Modeltransformation

RefactoringForward engineering

Reverse engineering

Page 3: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3

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

Player

Object design model before transformation

Object design model after transformation

AdvertiserLeagueOwner

User

+email:Address

Advertiser

+email:Address

Player

+email:Address

LeagueOwner

+email:Address

Page 4: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4

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

Before refactoring

public class Player {private String email;//...

}public class LeagueOwner {

private String eMail;//...

}public class Advertiser {

private String email_address;//...

}

After refactoringpublic class User {

private String email;}public class Player extends User {

//...}public class LeagueOwner extends

User {//...

}public class Advertiser extends

User {//...

}

Page 5: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5

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

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 class Advertiser extends User{

public Advertiser(String email) {this.email = email;

}}

After refactoring

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);

}}

Page 6: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6

Figure 10-5, Realization of User and LeagueOwner

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 */

}

public class LeagueOwner extends User {

private int maxNumLeagues;

public int getMaxNumLeagues() {

return maxNumLeagues;

}

public void setMaxNumLeagues

(int value) {

maxNumLeagues = value;

}

/* Other methods omitted */

}

User LeagueOwner

+maxNumLeagues:int

Object design model before transformation

Source code after transformation

+email:String+notify(msg:String)

Page 7: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7

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

Person SocialSecurity

number:String

Person

SSN:String

Object design model before transformation

Object design model after transformation

Page 8: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8

Figure 10-7, Delaying expensive computations

Image

filename:String

paint()

Image

filename:String

RealImage

data:byte[]

data:byte[]

ImageProxy

filename:String

image

1 0..1

Object design model before transformation

Object design model after transformation

paint()

paint() paint()

Page 9: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9

Figure 10-8, Realization of a unidirectional, one-to-one association

public class Advertiser {private Account account;public Advertiser() {

account = new Account();}public Account getAccount() {

return account;}

}

AccountAdvertiser11

Object design model before transformation

Source code after transformation

Page 10: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10

Figure 10-9, Realization of a bidirectional one-to-one association

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;

}

}

AccountAdvertiser11

Object design model before transformation

Source code after transformation

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;}

}

Page 11: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11

Figure 10-10, Realization of a bidirectional, one-to-many association

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);

}

}

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);

}

}

}

Advertiser Account1 *

Object design model before transformation

Source code after transformation

Page 12: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12

Figure 10-11, Realization of a bidirectional, many-to-many association

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);

}

}

}

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);

}

}

}

Tournament Player* *

Object design model before transformation

Source code after transformation

{ordered}

Page 13: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13

Figure 10-12, Realization of a bidirectional qualified association

PlayernickName0..1*

Object design model before forward engineering

League

Player**

Object design model before transformation

League

Page 14: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14

Figure 10-12, Realization of a bidirectional qualified association (continued)

public class League {

private Map players;

public void addPlayer(String nickName, Player p) {

if (!players.containsKey(nickName))

{

players.put(nickName, p);

p.addLeague(nickName, this);

}

}

}

public class Player {

private Map leagues;

public void addLeague

(String nickName, League l) {

if (!leagues.containsKey(l)) {

leagues.put(l, nickName);

l.addPlayer(nickName, this);

}

}

}

Source code after forward engineering

Page 15: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15

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

Tournament Player* *

Tournament Player* *

Object design model before transformation

Object design model after transformation

Statistics

1 1

+getAverageStat(name)+ getTotalStat(name)+ updateStats(match)

Statistics

+getAverageStat(name)+ getTotalStat(name)+ updateStats(match)

Page 16: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16

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 tournamentfor (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());

}}

}}

Page 17: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17

Figure 10-15, A complete implementation of the Tournament.addPlayer() contract.

Tournament

+isPlayerAccepted(p:Player):boolean+addPlayer(p:Player)

+getMaxNumPlayers():int

-maxNumPlayers: int«precondition»

!isPlayerAccepted(p)

«invariant»getMaxNumPlayers() > 0

«precondition»getNumPlayers() <

getMaxNumPlayers()

+getNumPlayers():int

«postcondition»isPlayerAccepted(p)

Page 18: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18

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

login email

“am384” “[email protected]

“js289” “[email protected]

User table

firstName

“alice”

“john”

“bd” “[email protected]”“bob”

Candidate keyCandidate key

Primary key

Page 19: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19

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.

login

“am384”

“am384”

League table

name

“tictactoeNovice”

“tictactoeExpert”

“js289”“chessNovice”

Foreign key referencing User table

Page 20: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20

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

User

+firstName:String+login:String+email:String

id:long firstName:text[25] login:text[8] email:text[32]

User table

Page 21: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21

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

id:long owner:long

LeagueLeagueOwner

LeagueOwner table League table

...... id:long

*1

Page 22: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 22

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

id

PlayerTournament

Tournament table Player table

**

tournament player

TournamentPlayerAssociation table

23 23

name ...

novice

24 expert

id

56

name ...

alice

79 john

56

23 79

Page 23: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 23

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

id

Player

User

LeagueOwner table Player table

User table

56

maxNumLeagues ...

12

id

79

credits ...

126

LeagueOwner

id

56

name ...

zoe

79 john

role

LeagueOwner

Player

maxNumLeagues credits

name

Page 24: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24

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

id

Player

User

LeagueOwner table Player table

56

maxNumLeagues ...

12

id

79

credits ...

126

LeagueOwner

maxNumLeagues credits

name

name

zoe

name

john

Page 25: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 25

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

Game

createStatistics()

ChessGameTicTacToeGame

TTTStatistics ChessStatistics

Tournament

Statistics

update()getStat()

DefaultStatistics

Page 26: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 26

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

Tournament

0..10..10..1

1 *

1

LeagueGame Player

Statistics

0..1

1

Page 27: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27

Figure 10-25, SimpleStatisticsVault object realizing the N-ary association of Figure 10-24.

TournamentControl

Statistics

update(match,player)getStatNames()

SimpleStatisticsVault

getStatisticsObject(game,player)getStatisticsObject(league,player)getStatisticsObject(tournament,player)

StatisticsView

Game

createStatistics()

getStat(name)

Page 28: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28

Figure 10-26, StatisticsVault as a Facade shielding the control and boundary objects from the Statistics storage and computation

TournamentControl

Statistics

update(match,player)getStatNames()

StatisticsVault

update(match)getStatNames(game)getStat(name,game,player)getStat(name,league,player)getStat(name,tournament,player)

StatisticsView

Game

createStatistics()

getStat(name)

Page 29: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29

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 {...}

}

Page 30: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30

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

scope:long

Statistics table

player:longscopetype:longid:long

id:long

StatisticCounters table

name:text[25] value:double

id:long

League table

...id:long

Game table

... id:long

Tournament table

...

Page 31: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31

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

Mailbox Folder Message

View

1 * *

*

1 *

Page 32: Art for Chapter 10, Mapping Models to Code

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32

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

League Tournament Round

Player

1 * *

*

1 *