page 1
play

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


  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 1 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 2 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 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3 Page 1

  2. Collapsing an object without interesting behavior 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 4 Delaying expensive computations Object design model before transformation Image filename:String data:byte[] paint() Object design model after transformation Image filename:String paint() image ImageProxy RealImage 1 0..1 filename:String data:byte[] paint() paint() Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5 Realization of a unidirectional, one-to-one association Object design model before transformation 1 1 Advertiser 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 6 Page 2

  3. Bidirectional one-to-one association Object design model before transformation 1 1 Advertiser Account Source code after transformation public class Advertiser { public class Account { /* The owner field is initialized /* The account field is initialized * during the constructor and * in the constructor and never * never modified. */ * modified. */ private Advertiser owner; private Account account; public Account(owner:Advertiser) public Advertiser() { { account = new Account(this); this .owner = owner; } } public Advertiser getOwner() { public Account getAccount() { return owner; return account; } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7 Bidirectional, one-to-many association Object design model before transformation 1 * Advertiser Account Source code after transformation public class Account { public class Advertiser { private Set accounts; private Advertiser owner; public void setOwner(Advertiser public Advertiser() { newOwner) { accounts = new HashSet(); if (owner != newOwner) { Advertiser old = owner; } public void owner = newOwner; addAccount(Account a) { if (newOwner != null) accounts.add(a); newOwner.addAccount( this ); a.setOwner( this ); } if (oldOwner != null) public void old.removeAccount( this ); removeAccount(Account a) { accounts.remove(a); } } a.setOwner( null ); } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8 } Bidirectional, many-to-many association Object design model before transformation {ordered} * * Tournament 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 players.add(p); (!tournaments.contains(t)) { p.addTournament( this ); tournaments.add(t); } t.addPlayer( this ); } } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9 Page 3

  4. 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 1 * 1 * * * View Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10 Bidirectional qualified association Object design model before transformation * * League Player nickName Object design model before forward engineering 0..1 * League nickName Player Source code after forward engineering Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11 Bidirectional qualified association (continued) Source code after forward engineering public class League { public class Player { private Map players; private Map leagues; public void addPlayer public void addLeague (String nickName, Player p) (String nickName, League l) { { if if (!leagues.containsKey(l)) (!players.containsKey(nickName { )) { leagues.put(l, players.put(nickName, nickName); p); l.addPlayer(nickName, p.addLeague(nickName, this ); this ); } } } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12 Page 4

  5. Transformation of an association class 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 1 Tournament Player * * Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13 Page 5

Recommend


More recommend