Overview Object-Oriented Software Engineering Chapter 10, Object design is situated between system design and Mapping Models to Code ♦ implementation. Object design is not very well understood and if not well done, leads to a bad system implementation. In this lecture, we describe a selection of transformations to ♦ Using UML, Patterns, and Java illustrate a disciplined approach to implementation to avoid system degradation. 1. Operations on the object model: Optimizations to address performance requirements � 2. Implementation of class model components: Realization of associations � Realization of operation contracts � 3. Realizing entity objects based on selected storage strategy Mapping the class model to a storage schema � Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2 Characteristics of Object Design Activities State of the Art of Model-based Software Engineering ♦ Developers perform transformations to the object model to ♦ The Vision improve its modularity and performance. � During object design we would like to implement a system that realizes the use cases specified during requirements elicitation and ♦ Developers transform the associations of the object model into system design. collections of object references, because programming ♦ The Reality languages do not support the concept of association. � Different developers usually handle contract violations differently. ♦ If the programming language does not support contracts, the � Undocumented parameters are often added to the API to address a developer needs to write code for detecting and handling requirement change. contract violations. � Additional attributes are usually added to the object model, but are ♦ Developers often revise the interface specification to not handled by the persistent data management system, possibly accommodate new requirements from the client. because of a miscommunication. � Many improvised code changes and workarounds that eventually ♦ All these activities are intellectually not challenging yield to the degradation of the system. � However, they have a repetitive and mechanical flavor that makes them error prone. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4 Model transformations Model Transformation Example Object design model before transformation Player LeagueOwner Advertiser +email:Address +email:Address +email:Address Forward engineering Refactoring Model Object design model transformation after transformation: User Reverse engineering +email:Address Model space Source code space LeagueOwner Advertiser Player Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6 Page 1
Refactoring Example: Pull Up Constructor Body Refactoring Example: Pull Up Field public class User { public class User { public class User { private String email; private String email; public User(String email) { } } this.email = email; } public class Player { public class Player extends User { } private String email; //... 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 public class LeagueOwner { } } User { private String eMail; //... public class LeagueOwner extends public class LeagueOwner extends //... User { User{ } } public LeagueOwner(String email) { public LeagueOwner(String email) { super(email); public class Advertiser { this.email = email; public class Advertiser extends } } private String email_address; User { } } //... //... public class Advertiser extends User { } public class Advertiser extendsUser{ } public Advertiser(String email) { public Advertiser(String email) { super(email); this.email = email; } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8 Forward Engineering Example Other Mapping Activities Object design model before transformation ♦ Optimizing the Object Design Model LeagueOwner User +maxNumLeagues:int +email:String ♦ Mapping Associations +notify(msg:String) ♦ Mapping Contracts to Exceptions Source code after transformation ♦ Mapping Object Models to Tables public class User { public class LeagueOwner extends User { private String email; private int maxNumLeagues; public String getEmail() { public int getMaxNumLeagues() { return email; return maxNumLeagues; } } public void setEmail(String value){ public void setMaxNumLeagues email = value; ( int value) { } public void notify(String msg) { maxNumLeagues = value; // .... } } /* Other methods omitted */ /* Other methods omitted */ } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10 Collapsing an object without interesting behavior Delaying expensive computations Object design model before transformation Object design model before transformation Image filename:String data:byte[] paint() Person SocialSecurity number:String ? Object design model after transformation Object design model after transformation ? Image filename:String paint() Person SSN:String image RealImage ImageProxy 1 0..1 filename:String data:byte[] paint() paint() Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12 Page 2
Other Mapping Activities Realization of a unidirectional, one-to-one association Object design model before transformation � Optimizing the Object Design Model 1 1 Advertiser Account � Mapping Associations ♦ Mapping Contracts to Exceptions ? ♦ Mapping Object Models to Tables 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 13 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14 Bidirectional one-to-one association Bidirectional, one-to-many association Object design model before transformation Object design model before transformation 1 * 1 1 Advertiser Account Advertiser Account Source code after transformation Source code after transformation public class Advertiser { public class Account { public class Advertiser { public class Account { private Advertiser owner; private Set accounts; /* The owner field is initialized /* The account field is initialized public Advertiser() { public void setOwner(Advertiser newOwner) * in the constructor and never * during the constructor and { accounts = new HashSet(); * never modified. */ * modified. */ if (owner != newOwner) { } private Account account; private Advertiser owner; Advertiser old = owner; public void addAccount(Account a) { owner = newOwner; accounts.add(a); public Advertiser() { public Account(owner:Advertiser) { if (newOwner != null) a.setOwner( this ); this .owner = owner; account = new Account(this); newOwner.addAccount( this ); } } } if (oldOwner != null) public void removeAccount(Account a) public Advertiser getOwner() { public Account getAccount() { { old.removeAccount( this ); return account; return owner; } accounts.remove(a); } } a.setOwner( null ); } } } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16 Bidirectional, many-to-many association Bidirectional qualified association Object design model before transformation Object design model before transformation {ordered} * * Tournament Player * * League Player nickName Source code after transformation public class Tournament { public class Player { private List players; private List tournaments; public Tournament() { public Player() { Object design model before forward engineering players = new ArrayList(); tournaments = new ArrayList(); 0..1 * } } League nickName Player 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 ); Source code after forward engineering } } } } } } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18 Page 3
Recommend
More recommend