0% found this document useful (0 votes)
7 views

4 Implementation

This document discusses problems that can occur when implementing an object design model in code and describes four types of transformations that can help address these problems: 1) Model transformations change the structure of a model but keep it conformant to the same metamodel. 2) Forward engineering transforms models into source code, but details must be manually defined in code. 3) Reverse engineering transforms existing code back into models. 4) Refactoring transforms code structure only to improve non-functional attributes without changing observable behavior. These transformations help bridge the gap between models and code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

4 Implementation

This document discusses problems that can occur when implementing an object design model in code and describes four types of transformations that can help address these problems: 1) Model transformations change the structure of a model but keep it conformant to the same metamodel. 2) Forward engineering transforms models into source code, but details must be manually defined in code. 3) Reverse engineering transforms existing code back into models. 4) Refactoring transforms code structure only to improve non-functional attributes without changing observable behavior. These transformations help bridge the gap between models and code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Implementation

Problems with implementing an Object


Design Model
 Programming languages do not support the concept of
UML associations
◦ 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 contracts in which the classes are
involved
 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 3
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 4
Program
4 Different Types of Transformations (in Java)
Yet Another
System Model
Another
System Model Forward
engineering

Refactoring
Model
transformation

Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5
Model Transformation
Takes as input a model conforming to a
meta model (for example the MOF
metamodel) and produces as output
another model conforming to the
metamodel
Model transformations are used in MDA
(Model Driven Architecture).

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6
Model Transformation Example
Object design model before transformation:

LeagueOwner Advertiser Player


+email:Address +email:Address +email:Address

Object design model


after transformation:
User
+email:Address

LeagueOwner Advertiser Player

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7
Program
(in Java)
Yet Another
System Model
Another
System Model Forward
engineering

Refactoring
Model
transformation

Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8
Refactoring : Pull Up Field
public class User {
public class Player { private String email;
private String email; }
//... public class Player extends User {
} //...
public class LeagueOwner {
}
private String eMail;
//... public class LeagueOwner extends User {
} //...
public class Advertiser { }
private String email_address; public class Advertiser extends User {
//... //...
} }.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
Refactoring Example: Pull Up Constructor Body
public class User {
public class User {
public User(String email) {
private String email;
this.email = email;
}
}
}
public class Player extends User { public class Player extends User {
public Player(String email) { public Player(String email) {
super(email);
this.email = email; }
} }
}
public class LeagueOwner extends User{ public class LeagueOwner extends User {
public LeagueOwner(String email) {
public LeagueOwner(String email) {
super(email);
this.email = email;
} }
}
}
public class Advertiser extends User{ public class Advertiser extends User {
public Advertiser(String email) { public Advertiser(String email) {
this.email = email; super(email);
}
} }.
}

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
Program
4 Different Types of Transformations (in Java)
Yet Another
System Model
Another
System Model Forward
engineering

Refactoring
Model
transformation

Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
Forward Engineering Example
Object design model before transformation:

User LeagueOwner
-email:String -maxNumLeagues:int
+getEmail():String +getMaxNumLeagues():int
+setEmail(e:String) +setMaxNumLeagues(n:int)
+notify(msg:String)

Source code after transformation:


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 e){ }
email = e; public void setMaxNumLeagues(int n) {
} maxNumLeagues = n;
public void notify(String msg) { }
// .... }
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12
More Forward Engineering
Examples
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 13
Collapsing Objects
Object design model before transformation:

Person SocialSecurity
number:String

Object design model after transformation:

Person
SSN:String

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 14
Examples of Model Transformations
and Forward Engineering
Model Transformations
◦ Goal: Optimizing the object design model
 Collapsing objects
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 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
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 17
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 18
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();
}
}

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19
Bidirectional one-to-one association
Object design model before transformation:
Advertiser 1 1 Account

+ getAcount (): Account + getOwner (): Advertiser


Source code after transformation:
public class Advertiser { public class Account {
/* account is initialized /* owner is initialized
* in the constructor and never * in the constructor and
* modified. */ * never modified. */
private Account account; private Advertiser owner;
public Advertiser() { public Account(owner:Advertiser) {
account = new Account(this); this.owner = owner;
} }
public Account getAccount() {
public Advertiser getOwner() {
return account;
return owner;
}
}
}
}
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20
Bidirectional one-to-many association
Object design model before transformation:
1 *
Advertiser Account
+ addAcount (a: Account) + setOwner (Advertiser: NewOwner)
+ removeAcount (a: Account)

Source code after transformation:


public class Advertiser { public class Account {
private Set accounts; private Advertiser owner;
public Advertiser() { public void setOwner(Advertiser newOwner) {
accounts = new HashSet(); if (owner != newOwner) {
} Advertiser old = owner;
public void addAccount(Account a) { owner = newOwner;
accounts.add(a); if (newOwner != null)
a.setOwner(this); newOwner.addAccount(this);
} if (oldOwner != null)
public void removeAccount(Account a) {
old.removeAccount(this);
accounts.remove(a);
}
a.setOwner(null);
} }
} }

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21
Bidirectional many-to-many
association
Object design model before transformation

Tournament * {ordered} * Player *


+ addPlayer(p: Player) +addTournament(t: Tournament)
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 (!tournaments.contains(t)) {
players.add(p); tournaments.add(t);
p.addTournament(this); t.addPlayer(this);
} }
} }
} }
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 22
Summary
Strategy for implementing associations:
◦ Be as uniform as possible
◦ Individual decision for each association
Example of uniform implementation
◦ 1-to-1 association:
 Role names are treated like attributes in the classes and translate to
references
◦ 1-to-many association:
 "Ordered many" : Translate to Vector
 "Unordered many" : Translate to Set
◦ Qualified association:
 Translate to Hash table

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

You might also like