Overview

advertisement
Overview


Object design is situated between system design and
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 illustrate
a disciplined approach to implementation to avoid system
degradation.
1.
2.
3.
Operations on the object model:

Optimizations to address performance requirements
Implementation of class model components:

Realization of associations

Realization of operation contracts
Realizing entity objects based on selected storage strategy

Mapping the class model to a storage schema
COP 3331
Object-Oriented Analysis and Design
1
Characteristics of Object Design Activities





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.
All these activities are error prone, due to coordination requirements.
COP 3331
Object-Oriented Analysis and Design
2
State of the Art of Model-based Software Engineering

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 that eventually yield to
the degradation of the system.
 Architectural Decay
COP 3331
Object-Oriented Analysis and Design
3
Model transformations
Forward engineering
Refactoring
Model
transformation
Reverse engineering
Model space
COP 3331
Object-Oriented Analysis and Design
Source code space
4
Model Transformation Example
Object design model before transformation
LeagueOwner
+email:Address
Object design model
after transformation:
Player
Advertiser
+email:Address
+email:Address
User
+email:Address
LeagueOwner
COP 3331
Advertiser
Object-Oriented Analysis and Design
Player
5
Refactoring Example: Pull Up Field
public class User {
private String email;
}
public class Player {
private String email;
//...
}
public class LeagueOwner {
private String eMail;
//...
}
public class Advertiser {
private String email_address;
//...
}
COP 3331
public class Player extends
User {
//...
}
public class LeagueOwner
extends User {
//...
}
public class Advertiser extends
User {
//...
}
Object-Oriented Analysis and Design
6
Refactoring Example: Pull Up Constructor Body
public class Player extends User {
public Player(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) {
this.email = email;
}
}
public class LeagueOwner extends User {
public LeagueOwner(String email) {
super(email);
}
}
public class Advertiser extendsUser{
public Advertiser(String email) {
this.email = email;
}
}
public class Advertiser extends User {
public Advertiser(String email) {
super(email);
}
}
public class User {
private String email;
}
COP 3331
Object-Oriented Analysis and Design
7
Forward Engineering Example
Object design model after transformation
LeagueOwner
+maxNumLeagues:int
User
+email:String
+notify(msg:String)
Source code after transformation
public class User {
private String email;
public String getEmail() {
return email;
}
public void setEmail(String value){
email = value;
}
public void notify(String msg) {
// ....
}
/* Other methods omitted */
}
COP 3331
public class LeagueOwner extends User {
private int maxNumLeagues;
public int getMaxNumLeagues() {
return maxNumLeagues;
}
public void setMaxNumLeagues
(int value) {
maxNumLeagues = value;
}
/* Other methods omitted */
}
Object-Oriented Analysis and Design
8
Other Mapping Activities




Optimizing the Object Design Model
Mapping Associations
Mapping Contracts to Exceptions
Mapping Object Models to Tables
COP 3331
Object-Oriented Analysis and Design
9
Collapsing an object without interesting behavior
Object design model before transformation
Person
SocialSecurity
number:String
Object design model after transformation
Person
SSN:String
COP 3331
Object-Oriented Analysis and Design
10
Delaying expensive computations
Object design model before transformation
Image
filename:String
data:byte[]
paint()
Object design model after transformation
Image
filename:String
paint()
ImageProxy
filename:String
paint()
COP 3331
image
1
Object-Oriented Analysis and Design
0..1
RealImage
data:byte[]
paint()
11
Other Mapping Activities




Optimizing the Object Design Model
Mapping Associations
Mapping Contracts to Exceptions
Mapping Object Models to Tables
COP 3331
Object-Oriented Analysis and Design
12
Realization of a unidirectional, one-to-one association
Object design model before transformation
Advertiser
1
1
Account
Source code after transformation
public class Advertiser {
private Account account;
public Advertiser() {
account = new Account();
}
public Account getAccount() {
return account;
}
}
COP 3331
Object-Oriented Analysis and Design
13
Bidirectional one-to-one association
Object design model before transformation
Advertiser
1
1
Account
Source code after transformation
public class Advertiser {
/* The account field is initialized
* in the constructor and never
* modified. */
private const Account account;
public Advertiser() {
account = new Account(this);
}
public Account getAccount() {
return account;
}
} // “const” available in C++
COP 3331
public class Account {
/* The owner field is initialized
* during the constructor and
* never modified. */
private Advertiser owner;
public Account(owner:Advertiser)
{
this.owner = owner;
}
public Advertiser getOwner() {
return owner;
}
}
Object-Oriented Analysis and Design
14
Bidirectional, one-to-many association
Object design model before transformation
1
Advertiser
*
Account
Source code after transformation
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);
}
}
}
COP 3331
Object-Oriented Analysis and Design
15
Bidirectional, many-to-many association
Object design model before transformation
Tournament
* {ordered}
*
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 (!tournaments.contains(t)) {
players.add(p);
tournaments.add(t);
p.addTournament(this);
t.addPlayer(this);
}
}
}
}
}
}
COP 3331
Object-Oriented Analysis and Design
16
Bidirectional qualified association
Object design model before transformation
League
*
*
Player
nickName
Object design model after forward engineering
League
nickName
*
0..1
Player
Source code after forward engineering
COP 3331
Object-Oriented Analysis and Design
17
Bidirectional qualified association (continued)
Source code after forward engineering
public class League {
private Map players;
public class Player {
private Map leagues;
public void addPlayer
(String nickName, Player p) {
if (!players.containsKey(nickName)) {
players.put(nickName, p);
p.addLeague(nickName, this);
}
}
}
public void addLeague
(String nickName, League l) {
if (!leagues.containsKey(l)) {
leagues.put(l, nickName);
l.addPlayer(nickName, this);
}
}
}
COP 3331
Object-Oriented Analysis and Design
18
Transformation of an association class
Object design model before transformation
Statistics
+getAverageStat(name)
+getTotalStat(name)
+updateStats(match)
Tournament
*
Player
*
Object design model after transformation: 1 class and two binary associations
Statistics
+getAverageStat(name)
+getTotalStat(name)
+updateStats(match)
1
1
Tournament
COP 3331
*
Object-Oriented Analysis and Design
Player
*
19
Other Mapping Activities




Optimizing the Object Design Model
Mapping Associations
Mapping Contracts to Exceptions
Mapping Object Models to Tables
COP 3331
Object-Oriented Analysis and Design
20
Exceptions as building blocks for contract violations




Many object-oriented languages, including Java do not include built-in
support for contracts.
However, we can use their exception mechanisms as building blocks
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.
 In this case acceptPlayer() should throw an exception of type KnownPlayer.
 See source code on next slide
COP 3331
Object-Oriented Analysis and Design
21
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() { // 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());
}
}
}
}
COP 3331
Object-Oriented Analysis and Design
22
Implementing a contract
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.
COP 3331
Object-Oriented Analysis and Design
23
A complete implementation of the Tournament.addPlayer()
contract
«invariant»
getMaxNumPlayers() > 0
«precondition»
!isPlayerAccepted(p)
Tournament
-maxNumPlayers: int
+getNumPlayers():int
+getMaxNumPlayers():int
+isPlayerAccepted(p:Player):boolean
+addPlayer(p:Player)
«precondition»
getNumPlayers() <
getMaxNumPlayers()
COP 3331
Object-Oriented Analysis and Design
«postcondition»
isPlayerAccepted(p)
24
Heuristics for Mapping Contracts to Exceptions
Be pragmatic, if you don’t have enough time.
 Omit checking code for postconditions and invariants.
 Usually redundant with the code accomplishing the functionality of the
class
 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.
COP 3331
Object-Oriented Analysis and Design
25
Other Mapping Activities




Optimizing the Object Design Model
Mapping Associations
Mapping Contracts to Exceptions
Mapping Object Models to Tables
COP 3331
Object-Oriented Analysis and Design
26
Mapping an object model to a relational database

UML object models can be mapped to relational databases:
 Some degradation occurs because all UML constructs must be mapped to a
single relational database construct - the table.

UML mappings






Each class is mapped to a table
Each class attribute is mapped onto a column in the table
An instance of a class represents a row in the table
A many-to-many association is mapped into its own table
A one-to-many association is implemented as buried foreign key
Methods are not mapped
COP 3331
Object-Oriented Analysis and Design
27
Mapping the User class to a database table
User
+firstName:String
+login:String
+email:String
User table
id:long
firstName:text[25]
COP 3331
login:text[8]
Object-Oriented Analysis and Design
email:text[32]
28
Primary and Foreign Keys


Any set of attributes that could be used to uniquely identify any data
record in a relational table is called a candidate key.
The actual candidate key that is used in the application to identify the
records is called the primary key.
 The primary key of a table is a set of attributes whose values uniquely
identify the data records in the table.

A foreign key is an attribute (or a set of attributes) that references the
primary key of another table.
COP 3331
Object-Oriented Analysis and Design
29
Example for Primary and Foreign Keys
Primary key
User table
firstName
login
email
“alice”
“am384”
“am384@mail.org”
“john”
“js289”
“john@mail.de”
“bob”
“bd”
“bobd@mail.ch”
Candidate key
Candidate key
League table
name
login
“tictactoeNovice”
“am384”
“tictactoeExpert”
“am384”
“chessNovice”
“js289”
Foreign key referencing User table
COP 3331
Object-Oriented Analysis and Design
30
Buried Association



Associations with multiplicity one can be implemented using a foreign
key.
For one-to-many associations we add a foreign key to the table
representing the class on the “many” end.
For all other associations we can select either class at the end of the
association.
COP 3331
Object-Oriented Analysis and Design
31
Buried Association

Associations with multiplicity “one” can be implemented using a
foreign key. Because the association vanishes in the table, we call this
a buried association.

For one-to-many associations we add the foreign key to the table
representing the class on the “many” end.

For all other associations we can select either class at the end of the
association.
LeagueOwner
1
*
LeagueOwner table
id:long
League table
...
COP 3331
League
id:long
Object-Oriented Analysis and Design
...
owner:long
32
Another Example for Buried Association
Transaction
Portfolio
*
transactionID
Foreign Key
Transaction Table
transactionID
COP 3331
portfolioID
Object-Oriented Analysis and Design
portfolioID
...
Portfolio Table
portfolioID ...
33
Mapping Many-To-Many Associations
In this case we need a separate table for the association
City
*
Serves
*
cityName
Airport
airportCode
airportName
Separate table for
“Serves” association
Primary Key
Airport Table
City Table
airportCode
IAH
HOU
ALB
MUC
HAM
cityName
Houston
Albany
Munich
Hamburg
COP 3331
airportName
Intercontinental
Hobby
Albany County
Munich Airport
Hamburg Airport
Object-Oriented Analysis and Design
Serves Table
cityName airportCode
IAH
Houston
HOU
Houston
ALB
Albany
MUC
Munich
HAM
Hamburg
34
Mapping the Tournament/Player association as a separate table
Tournament *
*
Player
Tournament table
id
name
23
novice
24
expert
...
Player table
TournamentPlayerAssociation
table
tournament
COP 3331
player
23
56
23
79
Object-Oriented Analysis and Design
35
id
name
56
alice
79
john
...
Realizing Inheritance


Relational databases do not support inheritance
Two possibilities to map UML inheritance relationships to a database
schema
 With a separate table (vertical mapping)
 The attributes of the superclass and the subclasses are mapped to
different tables
 By duplicating columns (horizontal mapping)
 There is no table for the superclass
 Each subclass is mapped to a table containing the attributes of the
subclass and the attributes of the superclass
COP 3331
Object-Oriented Analysis and Design
36
Realizing inheritance with a separate table
User
name
Player
credits
LeagueOwner
maxNumLeagues
User table
id
name
56
zoe
79
john
...
role
LeagueOwner
Player
LeagueOwner table
id
56
maxNumLeagues
Player table
...
12
COP 3331
Object-Oriented Analysis and Design
id
credits
79
126
37
...
Realizing inheritance by duplicating columns
User
name
LeagueOwner
maxNumLeagues
Player
credits
LeagueOwner table
id
name
56
zoe
Player table
maxNumLeagues ...
id
name
credits
12
79
john
126
COP 3331
Object-Oriented Analysis and Design
38
...
Comparison: Separate Tables vs Duplicated Columns

The trade-off is between modifiability and response time
 How likely is a change of the superclass?
 What are the performance requirements for queries?

Separate table mapping
 We can add attributes to the superclass easily by adding a column to the
superclass table
 Searching for the attributes of an object requires a join operation.

Duplicated columns
 Modifying the database schema is more complex and error-prone
 Individual objects are not fragmented across a number of tables, resulting
in faster queries
COP 3331
Object-Oriented Analysis and Design
39
Heuristics for Transformations

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.
COP 3331
Object-Oriented Analysis and Design
40
Realizing Associations (data structures)

1–1
 1  1 : class member variable
 1  1 : classes w. mutual references

1  many
 vector, list, deque
 ordered_set, hash_set
 ordered_map, hash_map (hash_table), assoc_array

many  many
 multimap (relation)
COP 3331
Object-Oriented Analysis and Design
41
Summary

Undisciplined changes => degradation of the system model
 Architectural Decay

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
COP 3331
Object-Oriented Analysis and Design
42
Supplemental Slides Start Here
COP 3331
Object-Oriented Analysis and Design
43
More Terminology

Roundtrip Engineering
 Forward Engineering + Reverse Engineering
 Inventory analysis: Determine the Delta between Object Model and
Code
 Together-J and Rationale provide tools for reverse engineering

Reengineering
 Used in the context of project management:
 Providing new functionality (customer dreams up new stuff) in the
context of new technology (technology enablers)
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
44
Statistics as a product in the Game Abstract Factory
Game
Tournament
createStatistics()
ChessGame
TicTacToeGame
Statistics
update()
getStat()
TTTStatisticsChessStatisticsDefaultStatistics
COP 3331
Object-Oriented Analysis and Design
45
N-ary association class Statistics
Statistics relates League, Tournament, and Player
Statistics
1
*
1 1
0..1
Game
0..1
League
COP 3331
Object-Oriented Analysis and Design
0..1
Tournament
0..1
Player
46
Realization of the Statistics Association
TournamentControl
StatisticsView
StatisticsVault
Statistics
update(match,playe
update(match)
getStatNames()
getStatNames(game)
getStat(name)
getStat(name,game,player)
getStat(name,league,player)
getStat(name,tournament,player)
Game
createStatistics()
COP 3331
Object-Oriented Analysis and Design
47
StatisticsVault as a Facade
TournamentControl
StatisticsView
StatisticsVault
update(match)
getStatNames(game)
getStat(name,game,player)
getStat(name,league,player)
Statistics
update(match,player)
getStatNames()
getStat(name)
Game
getStat(name,tournament,player)
createStatistics()
COP 3331
Object-Oriented Analysis and Design
48
Public interface of the StatisticsVault class
public class StatisticsVault {
public void update(Match m)
throws InvalidMatch, MatchNotCompleted {...}
public List getStatNames() {...}
public double getStat(String name, Game g, Player p)
throws UnknownStatistic, InvalidScope {...}
public double getStat(String name, League l, Player p)
throws UnknownStatistic, InvalidScope {...}
public double getStat(String name, Tournament t, Player p)
throws UnknownStatistic, InvalidScope {...}
}
COP 3331
Object-Oriented Analysis and Design
49
Database schema for the Statistics Association
Statistics table
id:long
scope:long scopetype:long
player:long
StatisticCounters table
id:long
name:text[25] value:double
Game table
id:long
League table
...
id:long
COP 3331
Object-Oriented Analysis and Design
Tournament table
...
id:long
50
...
Restructuring Activities



Realizing associations
Revisiting inheritance to increase reuse
Revising inheritance to remove implementation dependencies
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
51
Realizing Associations

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 Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
52
Unidirectional 1-to-1 Association
Object design model before transformation
ZoomInAction
MapArea
Object design model after transformation
ZoomInAction
MapArea
-zoomIn:ZoomInAction
+getZoomInAction()
+setZoomInAction(action)
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
53
Bidirectional 1-to-1 Association
Object design model before transformation
ZoomInAction
1
MapArea
1
Object design model after transformation
ZoomInAction
-targetMap:MapArea
+getTargetMap()
+setTargetMap(map)
Bernd Bruegge & Allen Dutoit
MapArea
-zoomIn:ZoomInAction
+getZoomInAction()
+setZoomInAction(action)
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
54
1-to-Many Association
Object design model before transformation
Layer
1
*
LayerElement
Object design model after transformation
Layer
LayerElement
-layerElements:Set
+elements()
+addElement(le)
+removeElement(le)
Bernd Bruegge & Allen Dutoit
-containedIn:Layer
+getLayer()
+setLayer(l)
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
55
Qualification
Object design model before transformation
Scenario
simname
*
0..1
SimulationRun
Object design model after transformation
Scenario
-runs:Hashtable
+elements()
+addRun(simname,sr:SimulationRun)
+removeRun(simname,sr:SimulationRun)
Bernd Bruegge & Allen Dutoit
SimulationRun
-scenarios:Vector
+elements()
+addScenario(s:Scenario)
+removeScenario(s:Scenario)
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
56
Increase Inheritance


Rearrange and adjust classes and operations to prepare for
inheritance
Abstract common behavior out of groups of classes
 If a set of operations or attributes are repeated in 2 classes the
classes might be special instances of a more general class.

Be prepared to change a subsystem (collection of classes) into a
superclass in an inheritance hierarchy.
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
57
Building a super class from several classes

Prepare for inheritance. All operations must have the same
signature but often the signatures do not match:
 Some operations have fewer arguments than others: Use
overloading (Possible in Java)
 Similar attributes in the classes have different names: Rename
attribute and change all the operations.
 Operations defined in one class but no in the other: Use virtual
functions and class function overriding.


Abstract out the common behavior (set of operations with same
signature) and create a superclass out of it.
Superclasses are desirable. They
 increase modularity, extensibility and reusability
 improve configuration management

Turn the superclass into an abstract interface if possible
 Use Bridge pattern
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
58
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 Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
59
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 Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
60
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 Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
61
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
Every student receives a number at the first day in in the
university.
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
62
Optimization Activities: Collapsing Objects
Matrikelnumber
Student
ID:String
Student
Matrikelnumber:String
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
63
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 Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
64
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 Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
65
Optimization Activities: Delaying Complex
Computations
Image
filename:String
data:byte[]
width()
height()
paint()
Image
filename:String
width()
height()
paint()
ImageProxy
filename:String
width()
height()
paint()
Bernd Bruegge & Allen Dutoit
image
1
0..1
RealImage
data:byte[]
width()
height()
paint()
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
66
Increase Inheritance

Rearrange and adjust classes and operations to prepare
for inheritance
 Generalization: Finding the base class first, then the sub
classes.
 Specialization: Finding the the sub classes first, then the
base class

Generalization is a common modeling activity. It allows to
abstract common behavior out of a group of classes
 If a set of operations or attributes are repeated in 2 classes
the classes might be special instances of a more general
class.

Always check if it is possible to change a subsystem
(collection of classes) into a superclass in an inheritance
hierarchy.
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
67
Generalization: Building a super class from
several classes
You need to prepare or modify your classes for
generalization.
 All operations must have the same signature but often
the signatures do not match:

 Some operations have fewer arguments than others: Use
overloading (Possible in Java)
 Similar attributes in the classes have different names:
Rename attribute and change all the operations.
 Operations defined in one class but no in the other: Use
virtual functions and class function overriding.

Superclasses are desirable. They
 increase modularity, extensibility and reusability
 improve configuration management

Many design patterns use superclasses
 Try to retrofit an existing model to allow the use of a design
pattern
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
68
Implement Associations

Two strategies for implementing associations:
1. Be as uniform as possible
2. Make an individual decision for each association

Example of a uniform implementation (often used by
CASE tools)
 1-to-1 association:

Role names are treated like attributes in the classes and
translate to references
 1-to-many association:

Always Translate into a Vector
 Qualified association:

Always translate into to Hash table
Bernd Bruegge & Allen Dutoit
Object-Oriented Software Engineering: Conquering Complex and Changing Systems
69
Download