SlideShare a Scribd company logo
1 of 44
UsingUML,Patterns,andJava
Object-OrientedSoftwareEngineering
Chapter 10,
Mapping Models to
Code
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2
Characteristics of Object Design Activities
• Developers try to improve modularity and
performance
• Developers need to transform associations into
references, because programming languages do
not support associations
• If the programming language does not support
contracts, the developer needs to write code for
detecting and handling contract violations
• Developers need to revise the interface
specification whenever the client comes up with
new requirements.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3
State of the Art:
Model-based Software Engineering
• The Vision
• During object design we build an object design model
that realizes the use case model and it is the basis for
implementation (model-driven design)
• The Reality
• Working on the object design model involves many
activities that are error prone
• Examples:
• A new parameter must be added to an operation.
Because of time pressure it is added to the source
code, but not to the object model
• Additional attributes are added to an entity object,
but the database table is not updated (as a result,
the new attributes are not persistent).
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4
Other Object Design Activities
• Programming languages do not support the
concept of a UML association
• The associations of the object model must be
transformed into collections of object references
• Many programming languages do not support
contracts (invariants, pre and post conditions)
• Developers must therefore manually transform contract
specification into source code for detecting and handling
contract violations
• The client changes the requirements during
object design
• The developer must change the interface specification of
the involved classes
• All these object design activities cause problems,
because they need to be done manually.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5
• Let us get a handle on these problems
• To do this we distinguish two kinds of spaces
• the model space and the source code space
• and 4 different types of transformations
• Model transformation,
• Forward engineering,
• Reverse engineering,
• Refactoring.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6
4 Different Types of Transformations
Source code space
Forward
engineering
Refactoring
Reverse
engineering
Model space
Model
transformation
System Model
(in UML)
Another
System Model
Program
(in Java)
Another
Program
Yet Another
System Model
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7
Model Transformation Example
Object design model before transformation:
Object design model
after transformation:
Advertiser
+email:Address
Player
+email:Address
LeagueOwner
+email:Address
PlayerAdvertiserLeagueOwner
User
+email:Address
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8
4 Different Types of Transformations
Source code space
Forward
engineering
Refactoring
Reverse
engineering
Model space
Model
transformation
System Model
(in UML)
Another
System Model
Program
(in Java)
Another
Program
Yet Another
System Model
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
Refactoring Example: Pull Up Field
public class Player {
private String email;
//...
}
public class LeagueOwner {
private String eMail;
//...
}
public class Advertiser {
private String
email_address;
//...
}
public class User {
private String email;
}
public class Player extends User {
//...
}
public class LeagueOwner extends
User {
//...
}
public class Advertiser extends
User {
//...
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
Refactoring Example: Pull Up Constructor Body
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 extendsUser{
public Advertiser(String email) {
this.email = 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);
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
4 Different Types of Transformations
Source code space
Forward
engineering
Refactoring
Reverse
engineering
Model space
Model
transformation
System Model
(in UML)
Another
System Model
Program
(in Java)
Another
Program
Yet Another
System Model
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12
Forward Engineering Example
public class User {
private String email;
public String getEmail() {
return email;
}
public void setEmail(String value){
email = value;
}
public void notify(String msg) {
// ....
}
}
public class LeagueOwner extends User {
private int maxNumLeagues;
public int getMaxNumLeagues() {
return maxNumLeagues;
}
public void setMaxNumLeagues
(int value) {
maxNumLeagues = value;
}
}
User
Object design model before transformation:
Source code after transformation:
-email:String
+getEmail():String
+setEmail(e:String)
+notify(msg:String)
LeagueOwner
-maxNumLeagues:int
+getMaxNumLeagues():int
+setMaxNumLeagues(n:int)
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13
More Examples of Model Transformations
and Forward Engineering
• Model Transformations
• Goal: Optimizing the object design model
• Collapsing objects
• Delaying expensive computations
• Forward Engineering
• Goal: Implementing the object design model in a
programming language
• Mapping inheritance
• Mapping associations
• Mapping contracts to exceptions
• Mapping object models to tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14
Collapsing Objects
Person SocialSecurity
number:String
Person
SSN:String
Object design model before transformation:
Object design model after transformation:
Turning an object into an attribute of another object is usually
done, if the object does not have any interesting dynamic behavior
(only get and set operations).
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15
Examples of Model Transformations and
Forward Engineering
• Model Transformations
• Goal: Optimizing the object design model
• Collapsing objects
• Delaying expensive computations
• Forward Engineering
• Goal: Implementing the object design model in a
programming language
• Mapping inheritance
• Mapping associations
• Mapping contracts to exceptions
• Mapping object models to tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16
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()
Proxy Pattern!
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17
Examples of Model Transformations and
Forward Engineering
• Model Transformations
• Goal: Optimizing the object design model
• Collapsing objects
• Delaying expensive computations
• Forward Engineering
• Goal: Implementing the object design model in a
programming language
• Mapping inheritance
• Mapping associations
• Mapping contracts to exceptions
• Mapping object models to tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18
Forward Engineering: Mapping a UML
Model into Source Code
• Goal: We have a UML-Model with inheritance.
We want to translate it into source code
• Question: Which mechanisms in the
programming language can be used?
• Let’s focus on Java
• Java provides the following mechanisms:
• Overriding of methods (default in Java)
• Final classes
• Final methods
• Abstract methods
• Abstract classes
• Interfaces
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19
Realizing Inheritance in Java
• Realization of specialization and generalization
• Definition of subclasses
• Java keyword: extends
• Realization of simple inheritance
• Overriding of methods is not allowed
• Java keyword: final
• Realization of implementation inheritance
• Overriding of methods
• No keyword necessary:
• Overriding of methods is default in Java
• Realization of specification inheritance
• Specification of an interface
• Java keywords: abstract, interface
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20
Examples of Model Transformations and
Forward Engineering
• Model Transformations
• Goal: Optimizing the object design model
Collapsing objects
Delaying expensive computations
• Forward Engineering
• Goal: Implementing the object design model in a
programming language
Mapping inheritance
• Mapping associations
• Mapping contracts to exceptions
• Mapping object models to tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21
Mapping Associations
1. Unidirectional one-to-one association
2. Bidirectional one-to-one association
3. Bidirectional one-to-many association
4. Bidirectional many-to-many association
5. Bidirectional qualified association.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 22
Unidirectional one-to-one association
AccountAdvertiser
11
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;
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 23
Bidirectional one-to-one association
public class Advertiser {
/* account is initialized
* in the constructor and never
* modified. */
private Account account;
public Advertiser() {
account = new
Account(this);
}
public Account getAccount() {
return account;
}
}
AccountAdvertiser 11
Object design model before transformation:
Source code after transformation:
public class Account {
/* owner is initialized
* in the constructor and
* never modified. */
private Advertiser owner;
publicAccount(owner:Advertiser) {
this.owner = owner;
}
public Advertiser getOwner() {
return owner;
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24
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 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 25
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
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 26
Bidirectional qualified association
Object design model after model transformation
PlayernickName
0..1*League
Player
**
Object design model before model transformation
League
nickName
Source code after forward engineering (see next slide)
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27
Bidirectional qualified association cntd.
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);
}
}
}
Object design model before forward engineering
PlayernickName
0..1*League
Source code after forward engineering
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28
Examples of Model Transformations and
Forward Engineering
• Model Transformations
• Goal: Optimizing the object design model
Collapsing objects
Delaying expensive computations
• Forward Engineering
• Goal: Implementing the object design model in a
programming language
Mapping inheritance
Mapping associations
• Mapping contracts to exceptions
• Mapping object models to tables
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29
Implementing Contract Violations
• Many object-oriented languages do not have
built-in support for contracts
• However, if they support exceptions, we can use
their exception mechanisms for signaling and
handling contract violations
• In Java we use the try-throw-catch mechanism
• Example:
• Let us assume the acceptPlayer() operation of
TournamentControl is invoked with a player who is
already part of the Tournament
• UML model (see slide 34)
• In this case acceptPlayer() in TournamentControl
should throw an exception of type KnownPlayer
• Java Source code (see slide 35).
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30
UML Model for Contract Violation Example
TournamentControl
Player
players*
Tournament
1
1
+applyForTournament()
Match
+playMove(p,m)
+getScore():Map
matches
*
+start:Date
+status:MatchStatus
-maNumPlayers:String
+start:Date
+end:Date
1
1
*
matches *
TournamentForm
*
*
+acceptPlayer(p)
+removePlayer(p)
+isPlayerAccepted(p)
Advertiser
sponsors *
*
*
*
*
+selectSponsors(advertisers):List
+advertizeTournament()
+acceptPlayer(p)
+announceTournament()
+isPlayerOverbooked():boolean
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31
Implementation in Java
public class TournamentForm {
private TournamentControl control;
private ArrayList players;
public void processPlayerApplications() {
for (Iteration i = players.iterator(); i.hasNext();) {
try {
control.acceptPlayer((Player)i.next());
}
catch (KnownPlayerException e) {
// If exception was caught, log it to console
ErrorConsole.log(e.getMessage());
}
}
}
}
TournamentControl
Player
players*
Tournament
1
1
+applyForTournament()
Match
+playMove(p,m)
+getScore():Map
matches
*
+start:Date
+status:MatchStatus
-maNumPlayers:String
+start:Date
+end:Date
1
1
*
matches*
TournamentForm
*
*
+acceptPlayer(p)
+removePlayer(p)
+isPlayerAccepted(p)
Advertiser
sponsors*
*
*
*
*
+selectSponsors(advertisers):List
+advertizeTournament()
+acceptPlayer(p)
+announceTournament()
+isPlayerOverbooked():boolean
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32
The try-throw-catch Mechanism 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() {
for (Iteration i = players.iterator(); i.hasNext();) {
try {
control.acceptPlayer((Player)i.next());
}
catch (KnownPlayerException e) {
// If exception was caught, log it to console
ErrorConsole.log(e.getMessage());
}
}
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 34
Implementing a Contract
• Check each precondition:
• Before the beginning of the method with a test to check
the precondition for that method
• Raise an exception if the precondition evaluates to false
• Check each postcondition:
• At the end of the method write a test to check the
postcondition
• Raise an exception if the postcondition evaluates to false.
If more than one postcondition is not satisfied, raise an
exception only for the first violation.
• Check each invariant:
• Check invariants at the same time when checking
preconditions and when checking postconditions
• Deal with inheritance:
• Add the checking code for preconditions and postconditions
also into methods that can be called from the class.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 35
A complete implementation of the
Tournament.addPlayer() contract
«precondition»
!isPlayerAccepted(p)
«invariant»
getMaxNumPlayers() > 0
«precondition»
getNumPlayers() <
getMaxNumPlayers()
Tournament
+isPlayerAccepted(p:Player):boolean
+addPlayer(p:Player)
+getMaxNumPlayers():int
-maxNumPlayers: int
+getNumPlayers():int
«postcondition»
isPlayerAccepted(p)
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 36
Heuristics: Mapping Contracts to Exceptions
• Executing checking code slows down your
program
• If it is too slow, omit the checking code for private and
protected methods
• If it is still too slow, focus on components with the
longest life
• Omit checking code for postconditions and
invariants for all other components.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 37
Heuristics for Transformations
• For any given transformation always use the
same tool
• Keep the contracts in the source code, not in the
object design model
• Use the same names for the same objects
• Have a style guide for transformations (Martin
Fowler)
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 38
Object Design Areas
1. Service specification
• Describes precisely each class interface
2. Component selection
• Identify off-the-shelf components and additional
solution objects
3. Object model restructuring
• Transforms the object design model to improve its
understandability and extensibility
4. Object model optimization
• Transforms the object design model to address
performance criteria such as response time or memory
utilization.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 39
Design Optimizations
• Design optimizations are an important part of
the object design phase:
• The requirements analysis model is semantically
correct but often too inefficient if directly implemented.
• Optimization activities during object design:
1. Add redundant associations to minimize access cost
2. Rearrange computations for greater efficiency
3. Store derived attributes to save computation time
• As an object designer you must strike a balance
between efficiency and clarity.
• Optimizations will make your models more obscure
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 40
Design Optimization Activities
1. Add redundant associations:
• What are the most frequent operations? ( Sensor data
lookup?)
• How often is the operation called? (30 times a month,
every 50 milliseconds)
2. Rearrange execution order
• Eliminate dead paths as early as possible (Use
knowledge of distributions, frequency of path traversals)
• Narrow search as soon as possible
• Check if execution order of loop should be reversed
3. Turn classes into attributes
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 41
Implement application domain classes
• To collapse or not collapse: Attribute or
association?
• Object design choices:
• Implement entity as embedded attribute
• Implement entity as separate class with associations to
other classes
• Associations are more flexible than attributes but
often introduce unnecessary indirection
• Abbott's textual analysis rules.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 42
Optimization Activities: Collapsing Objects
Student
Matrikelnumber
ID:String
Student
Matrikelnumber:String
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 43
To Collapse or not to Collapse?
• Collapse a class into an attribute if the only
operations defined on the attributes are Set()
and Get().
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 44
Design Optimizations (continued)
Store derived attributes
• Example: Define new classes to store information
locally (database cache)
• Problem with derived attributes:
• Derived attributes must be updated when base values
change.
• There are 3 ways to deal with the update problem:
• Explicit code: Implementor determines affected
derived attributes (push)
• Periodic computation: Recompute derived attribute
occasionally (pull)
• Active value: An attribute can designate set of
dependent values which are automatically updated
when active value is changed (notification, data
trigger)
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 45
Summary
• Four mapping concepts:
• Model transformation
• Forward engineering
• Refactoring
• Reverse engineering
• Model transformation and forward engineering
techniques:
• Optiziming the class model
• Mapping associations to collections
• Mapping contracts to exceptions
• Mapping class model to storage schemas

More Related Content

What's hot

Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1Shahzad
 
Design pattern
Design patternDesign pattern
Design patternOmar Isaid
 
JAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsJAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsRahul Malhotra
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)APU
 
Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldSaurabh Moody
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns pptmkruthika
 

What's hot (20)

Ch08lect3 ud
Ch08lect3 udCh08lect3 ud
Ch08lect3 ud
 
Ch09lect2 ud
Ch09lect2 udCh09lect2 ud
Ch09lect2 ud
 
Ch10lect2 ud
Ch10lect2 udCh10lect2 ud
Ch10lect2 ud
 
Ch08lect1 ud
Ch08lect1 udCh08lect1 ud
Ch08lect1 ud
 
Ch02lect1 ud
Ch02lect1 udCh02lect1 ud
Ch02lect1 ud
 
Ch03lect2 ud
Ch03lect2 udCh03lect2 ud
Ch03lect2 ud
 
Ch01lect1 ud
Ch01lect1 udCh01lect1 ud
Ch01lect1 ud
 
Ch05lect2 ud
Ch05lect2 udCh05lect2 ud
Ch05lect2 ud
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Design pattern
Design patternDesign pattern
Design pattern
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
JAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsJAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp concepts
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)
 
Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
 
08 component level_design
08 component level_design08 component level_design
08 component level_design
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
 

Viewers also liked

Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering conceptsKomal Singh
 
ASP.NET System design 2
ASP.NET System design 2ASP.NET System design 2
ASP.NET System design 2Sisir Ghosh
 
Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2wahab13
 
Object Modeling with UML: Behavioral Modeling
Object Modeling with UML: Behavioral ModelingObject Modeling with UML: Behavioral Modeling
Object Modeling with UML: Behavioral Modelingelliando dias
 
Ch9-Software Engineering 9
Ch9-Software Engineering 9Ch9-Software Engineering 9
Ch9-Software Engineering 9Ian Sommerville
 
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Isuru Perera
 
Ch7-Software Engineering 9
Ch7-Software Engineering 9Ch7-Software Engineering 9
Ch7-Software Engineering 9Ian Sommerville
 
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...Ahmed Al-Senosy Ph.D(cand),MSc,PMP,RMP
 
Texture mapping
Texture mapping Texture mapping
Texture mapping wahab13
 
Object Oriented Software Engineering
Object Oriented Software EngineeringObject Oriented Software Engineering
Object Oriented Software EngineeringAli Haider
 

Viewers also liked (14)

organization charts....rules
organization charts....rulesorganization charts....rules
organization charts....rules
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
 
Ch11lect2 ud
Ch11lect2 udCh11lect2 ud
Ch11lect2 ud
 
ASP.NET System design 2
ASP.NET System design 2ASP.NET System design 2
ASP.NET System design 2
 
Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2
 
Object Modeling with UML: Behavioral Modeling
Object Modeling with UML: Behavioral ModelingObject Modeling with UML: Behavioral Modeling
Object Modeling with UML: Behavioral Modeling
 
Ch9-Software Engineering 9
Ch9-Software Engineering 9Ch9-Software Engineering 9
Ch9-Software Engineering 9
 
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
 
Ch7-Software Engineering 9
Ch7-Software Engineering 9Ch7-Software Engineering 9
Ch7-Software Engineering 9
 
Ch7 implementation
Ch7 implementationCh7 implementation
Ch7 implementation
 
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
final thisis 2016,Ahmed Alsenosy,BPCPM of applying PMI Module in construction...
 
Texture mapping
Texture mapping Texture mapping
Texture mapping
 
Object Oriented Software Engineering
Object Oriented Software EngineeringObject Oriented Software Engineering
Object Oriented Software Engineering
 
Ch6 architectural design
Ch6 architectural designCh6 architectural design
Ch6 architectural design
 

Similar to Ch10lect1 ud

L14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.pptL14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.pptAronBalais1
 
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.pptL14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.pptAxmedMaxamuud6
 
L35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.pptL35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.pptgarimaarora436394
 
L35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.pptL35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.pptSaraj Hameed Sidiqi
 
Software Engineering Lec 2
Software Engineering Lec 2Software Engineering Lec 2
Software Engineering Lec 2Taymoor Nazmy
 
OOSE Ch1 Introduction
OOSE Ch1 IntroductionOOSE Ch1 Introduction
OOSE Ch1 IntroductionBenny Chen
 
L30_Project_Organization_ch13lect1.ppt
L30_Project_Organization_ch13lect1.pptL30_Project_Organization_ch13lect1.ppt
L30_Project_Organization_ch13lect1.pptDheerajMehlawat2
 
Discrete Event Simulation, CASE tool built using C#
Discrete Event Simulation, CASE tool built using C#Discrete Event Simulation, CASE tool built using C#
Discrete Event Simulation, CASE tool built using C#Ron Perlmuter
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Chap 6 - Software Architecture Part 1.ppt
Chap 6 - Software Architecture Part 1.pptChap 6 - Software Architecture Part 1.ppt
Chap 6 - Software Architecture Part 1.pptkhalidnawaz39
 
Chap 6 - Software Architecture Part 1.pptx
Chap 6 - Software Architecture Part 1.pptxChap 6 - Software Architecture Part 1.pptx
Chap 6 - Software Architecture Part 1.pptxssuser0ed5b4
 
Efficient Code Organisation
Efficient Code OrganisationEfficient Code Organisation
Efficient Code OrganisationSqueed
 
Lecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptxLecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptxAhmedAlAfandi5
 
Lecture 1 uml with java implementation
Lecture 1 uml with java implementationLecture 1 uml with java implementation
Lecture 1 uml with java implementationthe_wumberlog
 
Configuring in the Browser, Really!
Configuring in the Browser, Really!Configuring in the Browser, Really!
Configuring in the Browser, Really!Tim Geisler
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectColdFusionConference
 

Similar to Ch10lect1 ud (20)

L14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.pptL14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
 
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.pptL14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
L14_DesignGoalsSubsystemDecompositionc_ch06lect1.ppt
 
L35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.pptL35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.ppt
 
L35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.pptL35_LifecycleModeling_ch15lect1.ppt
L35_LifecycleModeling_ch15lect1.ppt
 
Lo 11
Lo 11Lo 11
Lo 11
 
Software Engineering Lec 2
Software Engineering Lec 2Software Engineering Lec 2
Software Engineering Lec 2
 
OOSE Ch1 Introduction
OOSE Ch1 IntroductionOOSE Ch1 Introduction
OOSE Ch1 Introduction
 
ch01lect1.ppt
ch01lect1.pptch01lect1.ppt
ch01lect1.ppt
 
L30_Project_Organization_ch13lect1.ppt
L30_Project_Organization_ch13lect1.pptL30_Project_Organization_ch13lect1.ppt
L30_Project_Organization_ch13lect1.ppt
 
Discrete Event Simulation, CASE tool built using C#
Discrete Event Simulation, CASE tool built using C#Discrete Event Simulation, CASE tool built using C#
Discrete Event Simulation, CASE tool built using C#
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Mkkailashbio
MkkailashbioMkkailashbio
Mkkailashbio
 
Chap 6 - Software Architecture Part 1.ppt
Chap 6 - Software Architecture Part 1.pptChap 6 - Software Architecture Part 1.ppt
Chap 6 - Software Architecture Part 1.ppt
 
Chap 6 - Software Architecture Part 1.pptx
Chap 6 - Software Architecture Part 1.pptxChap 6 - Software Architecture Part 1.pptx
Chap 6 - Software Architecture Part 1.pptx
 
Efficient Code Organisation
Efficient Code OrganisationEfficient Code Organisation
Efficient Code Organisation
 
OOSE-UNIT-1.pptx
OOSE-UNIT-1.pptxOOSE-UNIT-1.pptx
OOSE-UNIT-1.pptx
 
Lecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptxLecture 2 software components ssssssss.pptx
Lecture 2 software components ssssssss.pptx
 
Lecture 1 uml with java implementation
Lecture 1 uml with java implementationLecture 1 uml with java implementation
Lecture 1 uml with java implementation
 
Configuring in the Browser, Really!
Configuring in the Browser, Really!Configuring in the Browser, Really!
Configuring in the Browser, Really!
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an Architect
 

Ch10lect1 ud

  • 2. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2 Characteristics of Object Design Activities • Developers try to improve modularity and performance • Developers need to transform associations into references, because programming languages do not support associations • If the programming language does not support contracts, the developer needs to write code for detecting and handling contract violations • Developers need to revise the interface specification whenever the client comes up with new requirements.
  • 3. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3 State of the Art: Model-based Software Engineering • The Vision • During object design we build an object design model that realizes the use case model and it is the basis for implementation (model-driven design) • The Reality • Working on the object design model involves many activities that are error prone • Examples: • A new parameter must be added to an operation. Because of time pressure it is added to the source code, but not to the object model • Additional attributes are added to an entity object, but the database table is not updated (as a result, the new attributes are not persistent).
  • 4. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4 Other Object Design Activities • Programming languages do not support the concept of a UML association • The associations of the object model must be transformed into collections of object references • Many programming languages do not support contracts (invariants, pre and post conditions) • Developers must therefore manually transform contract specification into source code for detecting and handling contract violations • The client changes the requirements during object design • The developer must change the interface specification of the involved classes • All these object design activities cause problems, because they need to be done manually.
  • 5. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5 • Let us get a handle on these problems • To do this we distinguish two kinds of spaces • the model space and the source code space • and 4 different types of transformations • Model transformation, • Forward engineering, • Reverse engineering, • Refactoring.
  • 6. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6 4 Different Types of Transformations Source code space Forward engineering Refactoring Reverse engineering Model space Model transformation System Model (in UML) Another System Model Program (in Java) Another Program Yet Another System Model
  • 7. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7 Model Transformation Example Object design model before transformation: Object design model after transformation: Advertiser +email:Address Player +email:Address LeagueOwner +email:Address PlayerAdvertiserLeagueOwner User +email:Address
  • 8. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8 4 Different Types of Transformations Source code space Forward engineering Refactoring Reverse engineering Model space Model transformation System Model (in UML) Another System Model Program (in Java) Another Program Yet Another System Model
  • 9. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9 Refactoring Example: Pull Up Field public class Player { private String email; //... } public class LeagueOwner { private String eMail; //... } public class Advertiser { private String email_address; //... } public class User { private String email; } public class Player extends User { //... } public class LeagueOwner extends User { //... } public class Advertiser extends User { //... }
  • 10. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10 Refactoring Example: Pull Up Constructor Body 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 extendsUser{ public Advertiser(String email) { this.email = 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); } }
  • 11. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11 4 Different Types of Transformations Source code space Forward engineering Refactoring Reverse engineering Model space Model transformation System Model (in UML) Another System Model Program (in Java) Another Program Yet Another System Model
  • 12. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12 Forward Engineering Example public class User { private String email; public String getEmail() { return email; } public void setEmail(String value){ email = value; } public void notify(String msg) { // .... } } public class LeagueOwner extends User { private int maxNumLeagues; public int getMaxNumLeagues() { return maxNumLeagues; } public void setMaxNumLeagues (int value) { maxNumLeagues = value; } } User Object design model before transformation: Source code after transformation: -email:String +getEmail():String +setEmail(e:String) +notify(msg:String) LeagueOwner -maxNumLeagues:int +getMaxNumLeagues():int +setMaxNumLeagues(n:int)
  • 13. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13 More Examples of Model Transformations and Forward Engineering • Model Transformations • Goal: Optimizing the object design model • Collapsing objects • Delaying expensive computations • Forward Engineering • Goal: Implementing the object design model in a programming language • Mapping inheritance • Mapping associations • Mapping contracts to exceptions • Mapping object models to tables
  • 14. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14 Collapsing Objects Person SocialSecurity number:String Person SSN:String Object design model before transformation: Object design model after transformation: Turning an object into an attribute of another object is usually done, if the object does not have any interesting dynamic behavior (only get and set operations).
  • 15. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15 Examples of Model Transformations and Forward Engineering • Model Transformations • Goal: Optimizing the object design model • Collapsing objects • Delaying expensive computations • Forward Engineering • Goal: Implementing the object design model in a programming language • Mapping inheritance • Mapping associations • Mapping contracts to exceptions • Mapping object models to tables
  • 16. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16 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() Proxy Pattern!
  • 17. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17 Examples of Model Transformations and Forward Engineering • Model Transformations • Goal: Optimizing the object design model • Collapsing objects • Delaying expensive computations • Forward Engineering • Goal: Implementing the object design model in a programming language • Mapping inheritance • Mapping associations • Mapping contracts to exceptions • Mapping object models to tables
  • 18. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18 Forward Engineering: Mapping a UML Model into Source Code • Goal: We have a UML-Model with inheritance. We want to translate it into source code • Question: Which mechanisms in the programming language can be used? • Let’s focus on Java • Java provides the following mechanisms: • Overriding of methods (default in Java) • Final classes • Final methods • Abstract methods • Abstract classes • Interfaces
  • 19. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19 Realizing Inheritance in Java • Realization of specialization and generalization • Definition of subclasses • Java keyword: extends • Realization of simple inheritance • Overriding of methods is not allowed • Java keyword: final • Realization of implementation inheritance • Overriding of methods • No keyword necessary: • Overriding of methods is default in Java • Realization of specification inheritance • Specification of an interface • Java keywords: abstract, interface
  • 20. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20 Examples of Model Transformations and Forward Engineering • Model Transformations • Goal: Optimizing the object design model Collapsing objects Delaying expensive computations • Forward Engineering • Goal: Implementing the object design model in a programming language Mapping inheritance • Mapping associations • Mapping contracts to exceptions • Mapping object models to tables
  • 21. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21 Mapping Associations 1. Unidirectional one-to-one association 2. Bidirectional one-to-one association 3. Bidirectional one-to-many association 4. Bidirectional many-to-many association 5. Bidirectional qualified association.
  • 22. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 22 Unidirectional one-to-one association AccountAdvertiser 11 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; } }
  • 23. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 23 Bidirectional one-to-one association public class Advertiser { /* account is initialized * in the constructor and never * modified. */ private Account account; public Advertiser() { account = new Account(this); } public Account getAccount() { return account; } } AccountAdvertiser 11 Object design model before transformation: Source code after transformation: public class Account { /* owner is initialized * in the constructor and * never modified. */ private Advertiser owner; publicAccount(owner:Advertiser) { this.owner = owner; } public Advertiser getOwner() { return owner; } }
  • 24. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24 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 Account 1 * Object design model before transformation: Source code after transformation:
  • 25. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 25 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
  • 26. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 26 Bidirectional qualified association Object design model after model transformation PlayernickName 0..1*League Player ** Object design model before model transformation League nickName Source code after forward engineering (see next slide)
  • 27. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27 Bidirectional qualified association cntd. 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); } } } Object design model before forward engineering PlayernickName 0..1*League Source code after forward engineering
  • 28. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28 Examples of Model Transformations and Forward Engineering • Model Transformations • Goal: Optimizing the object design model Collapsing objects Delaying expensive computations • Forward Engineering • Goal: Implementing the object design model in a programming language Mapping inheritance Mapping associations • Mapping contracts to exceptions • Mapping object models to tables
  • 29. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29 Implementing Contract Violations • Many object-oriented languages do not have built-in support for contracts • However, if they support exceptions, we can use their exception mechanisms for signaling and handling contract violations • In Java we use the try-throw-catch mechanism • Example: • Let us assume the acceptPlayer() operation of TournamentControl is invoked with a player who is already part of the Tournament • UML model (see slide 34) • In this case acceptPlayer() in TournamentControl should throw an exception of type KnownPlayer • Java Source code (see slide 35).
  • 30. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30 UML Model for Contract Violation Example TournamentControl Player players* Tournament 1 1 +applyForTournament() Match +playMove(p,m) +getScore():Map matches * +start:Date +status:MatchStatus -maNumPlayers:String +start:Date +end:Date 1 1 * matches * TournamentForm * * +acceptPlayer(p) +removePlayer(p) +isPlayerAccepted(p) Advertiser sponsors * * * * * +selectSponsors(advertisers):List +advertizeTournament() +acceptPlayer(p) +announceTournament() +isPlayerOverbooked():boolean
  • 31. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31 Implementation in Java public class TournamentForm { private TournamentControl control; private ArrayList players; public void processPlayerApplications() { for (Iteration i = players.iterator(); i.hasNext();) { try { control.acceptPlayer((Player)i.next()); } catch (KnownPlayerException e) { // If exception was caught, log it to console ErrorConsole.log(e.getMessage()); } } } } TournamentControl Player players* Tournament 1 1 +applyForTournament() Match +playMove(p,m) +getScore():Map matches * +start:Date +status:MatchStatus -maNumPlayers:String +start:Date +end:Date 1 1 * matches* TournamentForm * * +acceptPlayer(p) +removePlayer(p) +isPlayerAccepted(p) Advertiser sponsors* * * * * +selectSponsors(advertisers):List +advertizeTournament() +acceptPlayer(p) +announceTournament() +isPlayerOverbooked():boolean
  • 32. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32 The try-throw-catch Mechanism 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() { for (Iteration i = players.iterator(); i.hasNext();) { try { control.acceptPlayer((Player)i.next()); } catch (KnownPlayerException e) { // If exception was caught, log it to console ErrorConsole.log(e.getMessage()); } } } }
  • 33. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 34 Implementing a Contract • Check each precondition: • Before the beginning of the method with a test to check the precondition for that method • Raise an exception if the precondition evaluates to false • Check each postcondition: • At the end of the method write a test to check the postcondition • Raise an exception if the postcondition evaluates to false. If more than one postcondition is not satisfied, raise an exception only for the first violation. • Check each invariant: • Check invariants at the same time when checking preconditions and when checking postconditions • Deal with inheritance: • Add the checking code for preconditions and postconditions also into methods that can be called from the class.
  • 34. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 35 A complete implementation of the Tournament.addPlayer() contract «precondition» !isPlayerAccepted(p) «invariant» getMaxNumPlayers() > 0 «precondition» getNumPlayers() < getMaxNumPlayers() Tournament +isPlayerAccepted(p:Player):boolean +addPlayer(p:Player) +getMaxNumPlayers():int -maxNumPlayers: int +getNumPlayers():int «postcondition» isPlayerAccepted(p)
  • 35. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 36 Heuristics: Mapping Contracts to Exceptions • Executing checking code slows down your program • If it is too slow, omit the checking code for private and protected methods • If it is still too slow, focus on components with the longest life • Omit checking code for postconditions and invariants for all other components.
  • 36. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 37 Heuristics for Transformations • For any given transformation always use the same tool • Keep the contracts in the source code, not in the object design model • Use the same names for the same objects • Have a style guide for transformations (Martin Fowler)
  • 37. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 38 Object Design Areas 1. Service specification • Describes precisely each class interface 2. Component selection • Identify off-the-shelf components and additional solution objects 3. Object model restructuring • Transforms the object design model to improve its understandability and extensibility 4. Object model optimization • Transforms the object design model to address performance criteria such as response time or memory utilization.
  • 38. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 39 Design Optimizations • Design optimizations are an important part of the object design phase: • The requirements analysis model is semantically correct but often too inefficient if directly implemented. • Optimization activities during object design: 1. Add redundant associations to minimize access cost 2. Rearrange computations for greater efficiency 3. Store derived attributes to save computation time • As an object designer you must strike a balance between efficiency and clarity. • Optimizations will make your models more obscure
  • 39. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 40 Design Optimization Activities 1. Add redundant associations: • What are the most frequent operations? ( Sensor data lookup?) • How often is the operation called? (30 times a month, every 50 milliseconds) 2. Rearrange execution order • Eliminate dead paths as early as possible (Use knowledge of distributions, frequency of path traversals) • Narrow search as soon as possible • Check if execution order of loop should be reversed 3. Turn classes into attributes
  • 40. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 41 Implement application domain classes • To collapse or not collapse: Attribute or association? • Object design choices: • Implement entity as embedded attribute • Implement entity as separate class with associations to other classes • Associations are more flexible than attributes but often introduce unnecessary indirection • Abbott's textual analysis rules.
  • 41. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 42 Optimization Activities: Collapsing Objects Student Matrikelnumber ID:String Student Matrikelnumber:String
  • 42. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 43 To Collapse or not to Collapse? • Collapse a class into an attribute if the only operations defined on the attributes are Set() and Get().
  • 43. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 44 Design Optimizations (continued) Store derived attributes • Example: Define new classes to store information locally (database cache) • Problem with derived attributes: • Derived attributes must be updated when base values change. • There are 3 ways to deal with the update problem: • Explicit code: Implementor determines affected derived attributes (push) • Periodic computation: Recompute derived attribute occasionally (pull) • Active value: An attribute can designate set of dependent values which are automatically updated when active value is changed (notification, data trigger)
  • 44. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 45 Summary • Four mapping concepts: • Model transformation • Forward engineering • Refactoring • Reverse engineering • Model transformation and forward engineering techniques: • Optiziming the class model • Mapping associations to collections • Mapping contracts to exceptions • Mapping class model to storage schemas

Editor's Notes

  1. All these activities are intellectually not challenging However, they have a repetitive and mechanical flavor that makes them error prone. Developers perform transformations to the object model to improve its modularity and performance. Developers transform the associations of the object model into collections of object references, because programming languages do not support the concept of association. If the programming language does not support contracts, the developer needs to write code for detecting and handling contract violations. Developers often revise the interface specification to accommodate new requirements from the client.
  2. The Vision During object design we would like to implement a system that realizes the use cases specified during requirements elicitation and system design. The Reality Different developers usually handle contract violations differently. Undocumented parameters are often added to the API to address a requirement change. Additional attributes are usually added to the object model, but are not handled by the persistent data management system, possibly because of a miscommunication. Many improvised code changes and workarounds are made ad hoc, which eventually leads to a severe degradation of the system.
  3. Question: Which pattern is being used here? Answer: The proxy pattern! Why: Image is an analysis domain object, the proxy pattern subclasses Imageproxy and RealImage are solution domain objects!
  4. For each of these mechanisms a project manager can specify heuristics to use them properly
  5. Java offers several techniques to realize the different types of inheritance: Realisation of simple inheritance also called strict inheritance
  6. If Advertiser accesses only Account, but not vice versa, the class Account can be made a local attribute in the Advertiser class
  7. Example of exception handling in Java. TournamentForm catches exceptions raised by Tournament and TournamentControl and logs them into an error console for display to the user. public void processPlayerApplications() { // Go through all the players 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 ErrorConsole.log(e.getMessage()); } } } }
  8. Example of exception handling in Java. TournamentForm catches exceptions raised by Tournament and TournamentControl and logs them into an error console for display to the user. public void processPlayerApplications() { // Go through all the players 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 ErrorConsole.log(e.getMessage()); } } } }
  9. For each operation in the contract, do the following Check precondition: Check the precondition before the beginning of the method with a test that raises an exception if the precondition is false. Check postcondition: Check the postcondition at the end of the method and raise an exception if the contract is violoated. If more than one postcondition is not satisfied, raise an exception only for the first violation. Check invariant: Check invariants at the same time as postconditions. Deal with inheritance: Encapsulate the checking code for preconditions and postconditions into separate methods that can be called from subclasses.
  10. Be pragmatic, if you don’t have enough time, change your tests in the following order: Omit checking code for postconditions and invariants. Often redundant, if you are confident that the code accomplishes the functionality of the method Not likely to detect many bugs unless written by a separate tester Omit the checking code for private and protected methods. Focus on components with the longest life Focus on entity objects, not on boundary objects associated with the user interface. Reuse constraint checking code. Many operations have similar preconditions. Encapsulate constraint checking code into methods so that they can share the same exception classes.
  11. For a given transformation use the same tool If you are using a CASE tool to map associations to code, use the tool to change association multiplicities. Keep the contracts in the source code, not in the object design model By keeping the specification as a source code comment, they are more likely to be updated when the source code changes. Use the same names for the same objects If the name is changed in the model, change the name in the code and or in the database schema. Provides traceability among the models Have a style guide for transformations By making transformations explicit in a manual, all developers can apply the transformation in the same way.
  12. Not covered in Fall 91 class
  13. Not covered in Fall 91 class
  14. Undisciplined changes =&amp;gt; degradation of the system model Four mapping concepts were introduced: Model transformation improves the compliance of the object design model with a design goal Forward engineering improves the consistency of the code with respect to the object design model Refactoring improves the readability or modifiability of the code Reverse engineering attempts to discover the design from the code. We reviewed model transformation and forward engineering techniques: Optiziming the class model Mapping associations to collections Mapping contracts to exceptions Mapping class model to storage schemas