CpSc 372: Introduction to Software Development

Representation Invariants
CpSc 372:
Introduction to
Software Engineering
Jason O. Hallstrom
[email protected]
Authorship Disclaimer. These slides are intended to serve as teaching instruments for an undergraduate course in Software
Engineering. While the slides were formatted by Dr. Hallstrom, the content is compiled from other sources, including the readings
listed on the course website, Dr. Pressman’s Software Engineering textbook, and various internet materials. In almost every case,
the ideas belong to someone other than Dr. Hallstrom. Indeed, text is often quoted verbatim without an explicit citation (to improve
the readability of the slides). The original authors retain all copyrights. If you are interested in citing any of the material in these
slides, please contact Dr. Hallstrom for the original source(s). DO NOT CITE THIS PRESENTATION. THE CONTENT SHOULD NOT BE
ATTRIBUTED TO DR. HALLSTROM. SEE DR. HALLSTROM IF YOU HAVE ANY QUESTIONS.
CpSc 372
Implementation Rules
When implementing an interface, we sometimes
impose rules to allow our method implementations
to work together.
modeled by:
Set of Object
represents
modeled by:
String of Object
Consider implementing the Set interface using an
implementation of Sequence.
CpSc 372
Interface Review
Recall the interface specifications that we’ve discussed.
public interface Set {
void clear();
void add(Object x);
void remove(Object x);
Object removeAny();
boolean isIn(Object x);
int getSize(); }
public interface Sequence {
void clear();
void add(int pos, Object x);
Object remove(int pos);
Object getElement(int pos);
int getLength(); }
modeled by:
Set of Object
modeled by:
String of Object
CpSc 372
Correct Implementation?
public class SetImpl1 implements Set {
/* representation: self = elements(self.items) */
private Sequence items;
public SetImpl1() {
items = new SequenceImpl1();
}
…
public void remove(Object x) {
for(int i = 0; i < items.getLength(); i++) {
if(items.getElement(i).equals(x)) {
items.remove(i);
return;
}
}
…
}
CpSc 372
Correct Implementation?
public class SetImpl1 implements Set {
/* representation: self = elements(self.items) */
private Sequence items;
public SetImpl1() {
items = new SequenceImpl1();
…
public void add(Object x) {
items.add(x);
items.add(x);
}
…
}
}
Considered in isolation, is the body of add() correct?
CpSc 372
What is a Representation Invariant?
A representation invariant characterizes a set
of rules that must be respected by every method
implementation.
A representation invariant is expressed as a
property that must be satisfied at the start and
end of every method invocation.
 Expressed
over concrete state space
 Satisfied at object creation
 Satisfied at method termination
CpSc 372
The Convention Clause
public class SetImpl1 implements Set {
/* representation:
self = elements(self.items)
convention:
*/
private Sequence items;
public SetImpl1() {
items = new SequenceImpl1();
…
}
}
CpSc 372
Another Example
What if we wanted to use binary search within the
body of isIn()?
public class SetImpl1 implements Set {
/* representation:
self = elements(self.items)
convention:
*/
…
}