Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Effective Java, Chapter 3: Methods Common to All Objects.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
Specifications Liskov Chapter 9 SWE 619 Last Updated Fall 2008.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Comp 302: Software Engineering Data Abstractions.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Data Abstraction Gang Qian Department of Computer Science University of Central Oklahoma.
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Effective Java: Methods Common to All Objects SWE 619: Fall 2008 Paul Ammann.
Understanding ADTs CSE 331 University of Washington.
Representation invariants and abstraction functions CSE 331 UW CSE.
Polymorphism SWE 619. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism Element.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Iteration Abstraction SWE Software Construction Fall 2009.
Abstraction, Specification and Verification. Comp 302, Spring Abstraction Hiding or overlooking detail that is not relevant for the task at hand.
CSE 331 Software Design & Implementation Hal Perkins Winter 2015 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, Dan Grossman,
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
Type Hierarchies. Type Hieararchy Why?: Want to define family of related types. At the top of the hierarchy, a type whose spec defines behavior common.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Mutability SWE 332 Fall 2015 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Representation Invariants and Abstraction Functions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Programming Techniques Lec05 Data Abstraction (Chapter 5) Software Engineering Fall 2005.
EECE 310: Software Engineering
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Specifications Liskov Chapter 9
Type Abstraction SWE Spring 2009.
Iteration Abstraction
Abstraction Functions and Representation Invariants
CSE 331 Software Design and Implementation
Representation Invariants and Abstraction Functions
Abstraction functions, Reasoning About ADTs
Type Abstraction Liskov, Chapter 7.
Data Abstraction David Evans cs205: engineering software
CSE 331 Software Design & Implementation
Effective Java, 3rd Edition Chapter 10: Exceptions
Iteration Abstraction
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Method Verification CS/SWE 332 Paul Ammann.
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
619 Final Review Last updated Spring 2010 © : Paul Ammann.
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Effective Java, Chapter 9: Exceptions
Type Abstraction SWE Spring 2013.
Reasoning about Data Abstractions
Basic Mutability Paul Ammann.
Method Verification Paul Ammann.
Presentation transcript:

Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann

2 Data Abstraction Abstract State (Client State) Representation State (Internal State) Methods Constructors (create objects) Producers (return immutable object) Mutators (change state) Observers (report about state)

3 Motivation Why data abstraction? Why hide implementation? Client code breaks Malicious or clueless clients don’t depend on/mess up the state Maintain properties like rep invariant Hence, encapsulation

4 IntSet and Poly IntSet {4,6} {}, {3,5,9}, {x 1, …, x n } Mutable Students find this easy to implement Poly 5+3x+8x 3, 4x+6x 2 c 0 +c 1 x+… Immutable Students find this harder to implement but a better approach

5 Specification of IntSet public class IntSet { //Overview: IntSets are mutable, unbounded sets of integers //A typical IntSet is {x 1, …, x n } //constructors public IntSet() //Effects: Initializes this to be empty //methods public void Insert (int x) // Modifies: this // Effects: Adds x to this, i.e., this_post = this + {x}

6 Specification of IntSet (2) public void remove (int x) //Modifies: this //Effects: Removes x from this, i.e., this_post=this –{x} public boolean isIn (int x) //Effects: If x is in this, returns true, else returns false public int size () //Effects: Returns the cardinality of this public int choose () throws EmptyException // Effects: If this is empty, throws EmptyException else // returns an arbitrary element of this

7 Remember! Don’t mention internal state in constructor or method specifications Always override toString() (don’t show the rep) Client should not see in-between state, all operations atomic this_post after execution of procedure

8 Set Notation – figure 5.3 A set is denoted {x1, … xn} Set union: t = s1 + s2 Set difference: t = s1 – s2 Set intersection: t = s1 & s2 Cardinality |s| denotes the size of set s Set membership: x in true if x element of set s Set former: t = { x | p(x)} is the set of all x such that p(x) is true These few notions are extremely powerful!

9 Specification of Poly public class Poly { // Overview: Polys are immutable polynomials with integer co- // -efficients. A typical Poly is c 0 +c 1 x+ … //constructors public Poly() // Effects: Initializes this to be the zero polynomial public Poly (int c, int n) throws NegativeExponentException // Effects: If n<0 throws NegativeExponentException // else initializes this to be the Poly cx n

10 Specification of Poly (2) //methods public int degree () //Effects: Returns the degree of this (the largest exponent // with non zero coeff. Returns 0 if this is the zero Poly public int coeff (int d) //Effects: Returns the coeff of the term of this with exp. d (what if d > degree? what if d < 0?) public Poly add (Poly q) throws NullPointerException //Effects: If q is null throws NPE else // returns the Poly this + q public Poly mul (Poly q) throws NPE //Effects: if q is null throws NPE, else returns this * q

11 Poly in detail 5+3x+8x 3 can’t build with the constructor Only zero or monomial NegativeExponentException No modifies clause: Immutable No mutators How does it support add, remove, multiply etc.?

12 Collections – A Slight Diversion Ordering Set Injective- List Bag Vector, List Y N YN DUPLICATESDUPLICATES One collection is often implemented with another.

13 Implementation (IntSet) Rep: Vector  Abstract state: Set Design decision (els never null) Use of special value in getIndex, justified? Design decision, duplicates in insert Why is remove as shown?

14 Implementation of IntSet (2) private Vector els; // the rep public void insert (int x) { //Modifies: this (NOT els!) //Effects: Adds x to the elements of this (NOT els!) Integer y = new Integer(x); if (getIndex(y) < 0) els.add(y); } private int getIndex(Integer x) { // Effects: If x is in this return index where x appears // else return -1 for (int i=0; i < els.size(); i++) if (x.equals(els.get(i))) return i; return -1; }

15 Implementation of IntSet (3) public void remove (int x) { //Modifies: this //Effects: Remove x from this int i = getIndex(new Integer(x)); if (i < 0) return; els.set(i, els.lastElement()); els.remove(els.size() - 1); } public int choose() { // Effects: If this empty throw EE else return arbitrary element of this if (els.size() == 0) throw new EE(“IntSet.choose”); return ((Integer) els.lastElement()).intValue(); }

16 Implementation of Poly (1) private int[] trms; private int deg; public Poly() { //Effects: Initializes this to be the zero polynomial trms = new int[1]; trms[0] = 0; deg = 0; } public Poly (int c, int n) throws IAE { //Effects if n < 0 throw IAE else initializes this to cx^n if (n < 0) throw new IAE(“Poly.constructor”); if (c ==0) { trms = new int[1]; trms[0] = 0; deg = 0; return} trms = new int[n+1]; deg = n; for (int i = 0; i < n; i++) trms[i] = 0; trms[n] = c; } private Poly (int n) { trms = new int[n+1] deg = n;}

17 Implementation of Poly (2) public int degree() { //Effects: Return degree of this, ie the largest exponent // with a nonzero coefficient. Returns 0 if this is the zero Poly return deg;} public coef (int d) { // Effects: Returns the coefficient of the term of this whose exp is d if (d deg) return 0; else return trms[d];} // implementations of add, sub, minus, mul

18 Abstraction Function Abstract state {x 1, …, x n } Representation state? Vector els = [y0, y1, …, yn] Representation state is designer’s choice. Clients don’t see rep (representation state), only see abstract state Clients don’t care for rep, as long as it satisfies properties of abstract state Implementer can change rep at any time!

19 Abstraction Function (IntSet) Abstraction function is a mapping from representation state to abstract state Abstraction function for IntSet: AF(c) =  c.els[i] | 0  i < c.els.size() } If duplicates allowed, what should AF(c) be? What if null is mapped to {}?

20 AF() for IntSet Abstract State (IntSet s) Representation State (Vector els) AF() els = [] els = null els = [1, 2] els = [2, 1] els = [“cat”, “hat”]els = [7, 1, 7] s = {} s = {1} s = {1, 2} s = {1, 7} els = [null, 5]

21 Rep Invariant Rep Invariant is the expression of combined restrictions on representation 1. c.els != null 2. No duplicates in c.els 3. All entries in c.els are Integers 4. No null entries English descriptions are fine! These are DESIGN DECISIONS. For example, we COULD allow duplicates...

22 Additional Methods 1. clone()  wrong in Liskov; we will use Bloch 2. toString()  Implementation of abstraction function 3. equals()  non standard wrt Java - understand why In terms of mutability– different for mutable and immutable types Bias towards correctness Three different things are required: 1. == // This checks if same object 2. equals() // for immutable only (same abstract state) 3. similar()// Mutable (same current abstract state) // Not implemented in Java

23 equals() Involves three important criteria: reflexivity, symmetry, transitivity Often difficult to implement For abstract extensions of instantiable superclasses, impossible to implement! Details later... toString  Should match with “typical object” Trick: Do it backwards - implement toString() first and then write the abstraction function.

SWE Mutable/Immutable Transform Stack example in Bloch Mutable Transform to an immutable version We should be fine with immutable stacks: Immutable Stack s = new Stack(); s = s.push(“cat”); s =s.push(“dog”); How to tranform:

SWE Mutator  Producer Consider a mutator method in class C: public void m(T t) or public S m(T t) What do the corresponding immutable methods look like? public C m(T t) or public ??? m(T t) Second needs to be split into two methods: Example: pop() vs. pop(), top() in Stack