12 - OOSC - Object Design (Specifying Interfaces)
12 - OOSC - Object Design (Specifying Interfaces)
Object-Oriented
SoftwareConstruction
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 2
Object Design: Closing the Gap
System
Problem
Application objects
Solution objects
Custom objects
Off-the-shelf components
Machine
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 3
Object Design – Interface Design
Activities:
Identify missing attributes and operations from analysis object and
subsystem service
Specify Visibility and Signatures
Specify Contracts (main focus in this lecture)
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 4
Developers play different Roles during
Object Design
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 5
Example for the relationship from viewpoint
of abstract class “Game”
Developers responsible for
Developers responsible for the implementation of Game are
the implementation of League are class implementors
class users of Game
League Game
1
*
Tournament TicTacToe Chess
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 7
Implementation of UML Visibility
in Java
Tournament
- maxNumPlayers: int
+ getMaxNumPlayers():int
+ getPlayers(): List
# removePlayer(p:Player)
+ acceptPlayer(p:Player)
+ isPlayerAccepted(p:Player):boolean
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 8
Information Hiding Heuristics
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 9
Information Hiding Design Principles
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 10
Add Type Signature Information
Hashtable
-numElements:int
+put()
+get()
+remove()
+containsKey()
+size()
Hashtable
Attributes and operations -numElements:int
without type information +put(key:Object,entry:Object)
are acceptable during analysis +get(key:Object):Object
+remove(key:Object)
+containsKey(key:Object):boolean
+size():int
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 11
Design by Contract
Idea: Contracts on a class enable caller (client) and callee (class
itself) to share the same assumptions (semantics) about the class.
Problem: Accurate Semantics cannot be added to standard UML
models discussed so far
Solution/Realization: Design By Contract by Betrand Meyer
(“Object-Oriented Software Construction”, Prentice Hall)
Process of developing software based on the notion of contracts
between objects
Contracts are expressed as assertions (predicates)
Assertions comprise preconditions, postconditions and invariants
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 12
Types of Assertions
Precondition:
Preconditions are predicates associated with a specific operation and
must be true before the operation is invoked.
Preconditions are used to specify constraints that a caller must meet
before calling an operation.
Postcondition:
Postconditions are predicates associated with a specific operation and
must be true after an operation is invoked.
Postconditions are used to specify constraints that the object (callee)
must ensure after the invocation of the operation.
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 13
Examples of Assertions
Assertion of List method “add”
PreCondition
list != null //List exists
key.instanceOf(String)
List
-list:Collection
+add(key:Object)
+create():void
+remove(key:Object)
+containsKey(key:Object):boolean
+size():int
PostCondition
size = size +1;
containsKey( key ) = true;
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 14
Design by Contract
Parties in the contract: class and client
PreCondition binds clients
PostCondition binds class
Contract important for Class User (Client) and Class Implementor
(class)
Contract entails benefits and obligations for both parties
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 15
Example
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 16
Class invariants and class correctness
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 17
Class invariants and class correctness
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 18
Invariant Rule
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 19
Design by Contract
Inheritance
What does inheritance mean in relation to Design by Contract?
Î Overridden methods must conceptually do the same job!
Contract Inheritance
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 20
Design by Contract
Inheritance
Demand no more:
A method of subclass is allowed to weaken the preconditions of
the method it overrides
An overridden method with a weaker precondition can handle more
cases than its superclass
Promise no less:
A method must ensure a stronger postcondition than the method
it overrides
An overridden method with a stronger postcondition ensures more
specific cases to the client upon return
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 21
Design by Contract
Inheritance – Example 1
A method of subclass is allowed to weaken the preconditions of the
method it overrides. Example (1)
List PreCondition P1
-list:Collection list != null //List exists
+add(key:Object)
key.instanceOf(String)
+create():void
+remove(key:Object)
+containsKey(key:Object):boolean
+size():int
Correct!
NewList
New PreCondition P2
+add(key:Object) list != null //List exists
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 22
Design by Contract
Inheritance – Example 2
A method of subclass is allowed to weaken the preconditions of the
method it overrides. Example (2)
List PreCondition P1
-list:Collection list != null //List exists
+add(key:Object)
+create():void key.instanceOf(String)
+remove(key:Object)
+containsKey(key:Object):boolean
+size():int
Violated!
NewList
New PreCondition P2
+add(key:Object) list != null //List exists
key.instanceOf(String)
Length(key) < 10
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 23
Design by Contract
Inheritance – Example 4
A method must ensure a stronger postcondition as the method it
overrides: Example:
List PostCondition P1
-list:Collection size = size +1;
+add(key:Object) containsKey( key ) = true;
+create():void
+remove(key:Object) size > 10
+containsKey(key:Object):boolean
+size():int
Violated!
NewList
PostCondition P2
+add(key:Object) size = size +1;
containsKey( key ) = true;
size > 2
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 24
Design by Contract
Inheritance – Example 3
A method must ensure a stronger postcondition as the method it
overrides: Example:
List PostCondition P1
-list:Collection size = size +1;
+add(key:Object) containsKey( key ) = true;
+create():void
+remove(key:Object) size > 3
+containsKey(key:Object):boolean
+size():int
Correct!
NewList
New PostCondition P2
+add(key:Object) size = size +1;
containsKey( key ) = true;
size > 9
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 25
Application of Design by Contract
Client T
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 26
Advantages of Design By Contract
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 27
Expressing constraints in UML Models
OCL 2.0 (Object Constraint Language) is part of UML 2.0
OCL allows constraints to be formally specified on single model elements
(attributes, operations, classes) or groups of model elements
(associations and participating classes)
A constraint is expressed as an OCL expression returning the value true
or false.
OCL is not a procedural language (cannot constrain control flow).
More Info:
Introduction of OCL for Together
http://bdn1.borland.com/devcon05/article/1,2006,33187,00.html
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 28
Expressing constraints in UML Models
OCL expressions for Hashtable operation put():
The context keyword indicates the object to which the
expression is valid
Context is a class
Invariant:
context Hashtable inv: numElements >= 0 OCL expression
Precondition:
context Hashtable::put(key, entry) pre:!containsKey(key)
Post-condition:
context Hashtable::put(key, entry) post: containsKey(key) and
get(key) = entry
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 29
Expressing constraints in UML Models
<<invariant>>
numElements >= 0
<<precondition>> HashTable
!containsKey(key) numElements:int <<postcondition>>
get(key) == entry
<<precondition>> put(key,entry:Object)
containsKey(key) get(key):Object
remove(key:Object)
<<precondition>> containsKey(key:Object):boolean
containsKey(key) size():int <<postcondition>>
!containsKey(key)
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 30
Contract for acceptPlayer in
Tournament
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 31
Contract for acceptPlayer in
Tournament
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 32
OCL Annotations
JavaDoc (iContract)
public class Tournament { /** The acceptPlayer() operation
/** The maximum number of players * assumes that the specified
* is positive at all times. * player has not been accepted
* @invariant maxNumPlayers > 0 * in the Tournament yet.
*/ * @pre !isPlayerAccepted(p)
* @pre getNumPlayers()<maxNumPlayers
private int maxNumPlayers; * @post isPlayerAccepted(p)
* @post getNumPlayers() =
/** The players List contains * @pre.getNumPlayers() + 1
* references to Players who are */
* are registered with the public void acceptPlayer (Player p)
* Tournament. */ {…}
private List players;
/** The removePlayer() operation
/** Returns the current number of * assumes that the specified player
* players in the tournament. */ * is currently in the Tournament.
* @pre isPlayerAccepted(p)
public int getNumPlayers() {…} * @post !isPlayerAccepted(p)
* @post getNumPlayers() =
/** Returns the maximum number of @pre.getNumPlayers() - 1
* players in the tournament. */ */
public int getMaxNumPlayers() {…} public void removePlayer(Player p) {…}
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 33
Constraints can involve more than
one class
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 34
Types of Navigation through a Class
Diagram
*
Player
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 35
Example: League, Tournament and
Player
League
*
+start:Date
+end:Date
+getActivePlayers()
* tournaments
Tournament
+start:Date
+end:Date
+acceptPlayer(p:Player)
* tournaments
* players
players
Player
*
+name:String
+email:String
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 36
Model Refinement with 2 additional
Constraints
♦ A Tournament’s planned duration must be under one week.
♦ Players can be accepted in a Tournament only if they are already
registered with the corresponding League.
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 37
OCL Collections
League chess:League
*
Player Bob:Player Isa:Player Marc:Player
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 38
OCL Collections
WM: EM:
* Tournament Tournament
Tournament
*
Player
league.tournament.player = {Bob, Isa, Marc,Isa}
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 39
OCL Collections
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 40
Specifying the Model Constraints
league.players->includes(p) +start:Date
+end:Date
+acceptPlayer(p:Player)
* tournaments
* players
players
Player
*
+name:String
+email:String
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 41
OCL supports Quantification
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 42
Realization of Constrains in
Programming Language: Java
♦ Often realized in terms of conventional exception handling
♦ Assertions to check pre- and postconditions within methods can be
used since JDK 1.4
♦ The validation of Assertions must be enabled (is disabled by default)
Start:
Start:
java Assertions
x=0
java.lang.ArithmeticException:
/ by zero …
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 44
Summary
♦ There are three different roles for developers during object design
Class user, class implementor and class extender
♦ During object design - and only during object design - we specify
visibility rules
♦ Constraints are boolean expressions on model elements
♦ Contracts are constraints on a class enable class users, implementors
and extenders to share the same assumption about the class
(“Design by contract”)
♦ OCL is a language that allows us to express constraints on UML
models
♦ Complicated constratins involving more than one class, attribute or
operation can be expressed with 3 basic navigation types.
Armin B. Cremers, Sascha Alda & Tobias Rho (based on Bruegge & Dutoit) Object-Oriented Software Construction 45