Title: Topics for Week 7
1Specifying Object Interfaces
2Major tasks in this stage --are there any
missing attributes or operations? --how can we
reduce coupling, make interface simple?
what operations are public/private/protected?
what are input parameters of each operation?
what is the return type of each
operation? --what contracts are there? (pre
and post conditions, e.g.)
3- Developers participating in class specification
- implementerdesigns internal data structures
and code for the class - userwill use the class as an applicationmust
know boundary (interface specification) for the
client class they are developing - extenderdevelops specialized version of the
classinterface specifications provide
constraints to this developer
4Preconditions, postconditions, invariants
contracts useful in white box and black box
testing, include preconditions, postconditions,
and invariants (loop or class, e.g.). Example
how do you ensure --there is no division by
zero? --array bounds are not
exceeded? --I/O error does not cause
system crash? --faulty / missing
component does not cause system crash?
5Preconditions, postconditions, invariants--definit
ions
precondition logical condition that a caller of
an operation guarantees before making the
call postcondition logical condition that an
operation guarantees upon completion invariant
logical condition that is preserved by
transformations example loop invariant is
preserved by the transformations effected by the
loop instructions
6Example--stacks, queues, division
example what would be preconditions for
common stack operations? What would be
postconditions? Push Pop Isempty? Top wh
at about a queue? What about division?
7Loop invariant
example does this program compute an for n gt
0? float pow(float a, int n) float r 1
while (n gt 0) if (n2 0) a
aann/2 else rra nn-1
return r "proof" using an invariant that
this program is correct let ain , nin be inputs
invariant r x an ain nin proof 1.
Invariant holds before entering loop first
time 2. Invariant holds after iteration i if it
held at iteration i-1 3. So, by induction,
invariant is true for n gt 0 3. Now, if loop
terminates, n 0, so we program does compute an
8Other invariants
example class invariant is preserved by all
class operations e.g., the size
of an array is always gt 0 interface
invariants for public sections
(users) implementation invariants for class
implementations (designers, coders)
9Invariants--examples
example suppose we have a bounded stack of
floating point numbers interface invariant
after a push, the stack is no longer
empty s.push(x) gt not (s.empty) if the stack is
not full and we push an element on the stack,
then calling pop returns that element not(s.full)
and s.push(x) ys.pop gt x y these make no
reference to the particular implementation
10Invariants--examples (cont.)
Implementation invariant example if we
implement the stack as a bounded array then an
invariant is that the stack pointer is within the
array bounds using invariants in
program c assert c , java error
checking--(e.g., try, catch) note including
checking increases program length, decreases
efficiency
11Expressing constraints Can use natural language
or a special language such as OCL (Object
Constraint Language) Constraint is a boolean
expression, returns value true or
false Constraint can be --attached to an entry
in the CRC card if desired (example figure
9.4)leads to cluttered documents --specified by
keyword context
12Examples from text (this form or syntax in figure
9-5, page 360 should be used in object comments
in project) context Tournament
inv self.getMaxNumPlayers( ) gt 0 context
TournamentacceptPlayer(pPlayer) pre !is
PlayerAccepted(p) context TournamentacceptPlaye
r(pPlayer) pre getNumPlayers( ) lt
getmaxNumPlayers( ) context TournamentacceptPl
ayer(pPlayer) post isPlayerAccepted(p)
13Useful OCL syntax Collections of objects
distinguishes among sets, sequences, bags --set
for a single association --sequence used for
navigating a single ordered association --bag
may contain the same object multiple times
(differs from set) Useful operations on
collections Size includes(object)member select
(expression)members which satisfy given
condition union(collection) interstction(collectio
n) asSet(collection)returns the set of members
of the collection Quantifiers forAll exists
We will revisit these concepts when we discuss
formal methods
14Object designsteps ---Identify missing
attributes and operations ---specify types,
signatures (e.g. is a collection ordered or
not?), and visibility of each attribute and
operation --specify pre and post
conditions --specify invariants --note any
inherited contracts --document the process in
the Object design Document --assign development
responsibilities to developer team