Page 1 Collapsing an object without interesting behavior Object - - PDF document

page 1
SMART_READER_LITE
LIVE PREVIEW

Page 1 Collapsing an object without interesting behavior Object - - PDF document

Podcast Ch10-02 Title : Optimizations, Mapping Associations Description : Some Optimization Techniques; One-to-One Association; One-to-Many Association; Many-to-Many Association; Qualified Association Participants : Barry Kurtz


slide-1
SLIDE 1

Page 1

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

Podcast Ch10-02

♦Title: Optimizations, Mapping Associations ♦Description: Some Optimization

Techniques; One-to-One Association; One-to-Many Association; Many-to-Many Association; Qualified Association

♦Participants: Barry Kurtz (instructor);

Brandon Winters, Sara Hyde, Cheng Vue, Dan Baehr (students)

♦Textbook: Object-Oriented Software

Engineering: Using UML, Patterns and Java by Bernd Bruegge and Allen H. Dutoit

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

Some optimizations - 1

♦ Avoid frequent repeated association traversals

Multiple association traversals can lead to code such as method1().method2(). …. methodn() The sequence diagram can be used to identify such traversals See if a direct connection is possible to improve efficiency

♦ Try to make “many” associations more efficient

Try to use a qualified association to reduce multiplicity to one Try to use ordering or indexing to decrease access time

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

Some optimizations - 2

♦ Try to avoid misplaced attributes

Inefficient due to the need for get and set methods Try bringing the attribute into the calling class

♦ Catch the result of expensive computations

Example, statistics in the Arena example are only updated after a match is completed This only has to be calculated once at the end of each match and not repeatedly until another match is completed

slide-2
SLIDE 2

Page 2

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

Collapsing an object without interesting behavior

Person SocialSecurity number:String Person SSN:String

Object design model before transformation Object design model after transformation

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

Delaying expensive computations

Object design model before transformation Object design model after transformation

Image filename:String paint() data:byte[] Image filename:String RealImage data:byte[] ImageProxy filename:String image 1 0..1 paint() paint() paint()

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

Realization of a unidirectional,

  • ne-to-one association

Account Advertiser 1 1

Object design model before transformation Source code after transformation

public class Advertiser { private Account account; public Advertiser() { account = new Account(); } public Account getAccount() { return account; } }

slide-3
SLIDE 3

Page 3

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

Bidirectional one-to-one association

public class Advertiser { /* 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; } }

Account Advertiser 1 1

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

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

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;

  • wner = newOwner;

if (newOwner != null) newOwner.addAccount(this); if (oldOwner != null)

  • ld.removeAccount(this);

} } }

Advertiser Account 1 *

Object design model before transformation Source code after transformation

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

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

Source code after transformation

{ordered}

Object design model before transformation

slide-4
SLIDE 4

Page 4

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

Exercise ch10-02-01

♦ Apply the transformations described above (and

in section 10.4.2 of the textbook) to the classes shown below. Assume all associations are bidirectional and that they can change during the lifetime of each object. Write the source code needed to manage the associations, including class, field, and method declarations, method bodies, and visibility. (Note – this is exercise 10- 2 on pages 430-431)

Mailbox Folder Message View 1 * * * 1 *

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

Bidirectional qualified association

Object design model before forward engineering

Player nickName 0..1 * League Player * *

Object design model before transformation

League nickName

Source code after forward engineering

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

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

slide-5
SLIDE 5

Page 5

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

Transformation of an association class

Tournament Player * *

Object design model before transformation Object design model after transformation

Statistics +getAverageStat(name) +getTotalStat(name) +updateStats(match) Tournament Player * * 1 1 Statistics +getAverageStat(name) +getTotalStat(name) +updateStats(match)