Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representation Invariants CpSc 372: Introduction to Software Engineering Jason O. Hallstrom Authorship Disclaimer. These slides.

Similar presentations


Presentation on theme: "Representation Invariants CpSc 372: Introduction to Software Engineering Jason O. Hallstrom Authorship Disclaimer. These slides."— Presentation transcript:

1 Representation Invariants CpSc 372: Introduction to Software Engineering Jason O. Hallstrom jasonoh@cs.clemson.edu 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.

2 CpSc 372 Implementation Rules When implementing an interface, we sometimes impose rules to allow our method implementations to work together. modeled by: String of Object modeled by: Set of Object represents Consider implementing the Set interface using an implementation of Sequence.

3 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: String of Object modeled by: Set of Object

4 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; } … }

5 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); } … } Considered in isolation, is the body of add() correct?

6 CpSc 372 What is a Representation Invariant? 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 A representation invariant characterizes a set of rules that must be respected by every method implementation.

7 CpSc 372 The Convention Clause public class SetImpl1 implements Set { /* representation: self = elements(self.items) convention: */ private Sequence items; public SetImpl1() { items = new SequenceImpl1(); } … }

8 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: */ … }


Download ppt "Representation Invariants CpSc 372: Introduction to Software Engineering Jason O. Hallstrom Authorship Disclaimer. These slides."

Similar presentations


Ads by Google