1 / 34

Data Abstraction: Specifying and Implementing Abstract Data Types

This lecture discusses the concept of data abstraction and how to specify and implement abstract data types in Java. It covers the advantages and disadvantages of data abstraction, using examples such as CellState and StringSet. The lecture also explores the components of data abstractions, including creators, producers, observers, and mutators.

Télécharger la présentation

Data Abstraction: Specifying and Implementing Abstract Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4: Data Abstraction David Evans http://www.cs.virginia.edu/~evans CS201j: Engineering Software University of Virginia Computer Science

  2. Menu • Data Abstraction • Specifying Abstract Data Types • Implementing Abstract Data Types CS 201J Fall 2002

  3. Managing Complexity • Modularity • Divided problem into procedures • Used specifications to separate what from how • A big program can have thousands of procedures CS 201J Fall 2002

  4. Data Abstraction • We need new data types, not just procedures • How would PS2 work without the StringTable type? • We could make procedures, but what would you pass to them? • Organize program around abstract data types CS 201J Fall 2002

  5. Abstract Data Types • Separate what you can do with data from how it is represented • Client interacts with data through provided operations according to their specifications • Implementation chooses how to represent data and implement its operations CS 201J Fall 2002

  6. Data Abstraction in Java • A class defines a new data type • Use private instance variables to hide the choice of representation • private declarations are only visible inside the class CS 201J Fall 2002

  7. Up and Down Clients manipulate an abstract data type by calling its operations (methods and constructors) clients down up Abstract Type Concrete Representation class implementation The representation of an abstract data type is visible only in the class implementation. CS 201J Fall 2002

  8. Example: CellState public class CellState { // OVERVIEW: A CellState is an immutable // object that represents the state of a cell, // either alive or dead. static public CellState createAlive() // EFFECTS: Returns an alive cell state. static public CellState createDead () // EFFECTS: Returns a dead cell state. public boolean isAlive () // EFFECTS: Returns true iff this is alive. } CS 201J Fall 2002

  9. Cell State Representation private boolean alive; clients cs.isAlive () CellState down up Abstract Type Concrete Representation boolean alive; class implementation public boolean isAlive () { return alive; } CS 201J Fall 2002

  10. Advantages/Disadvantages • More code to write and maintain • Run-time overhead (time to call method) • Client doesn’t need to know about representation • Suppose we want to add more states (e.g., cells that were just born) CS 201J Fall 2002

  11. StringSet Example • StringSet abstract data type: represent a set of strings • Support mathematical set operations: insert, isIn, size • Create an empty set CS 201J Fall 2002

  12. Specifying Abstract Data Types • Overview: what does the type represent • Mutability/Immutability A StringSet is a mutable set of Strings. • Abstract Notation A typical StringSet is { x1, …, xn }. • Operations: specifications for constructors and methods clients use • Describe in terms of abstract notation introduced in overview. CS 201J Fall 2002

  13. StringSet Specification public class StringSet // OVERVIEW: StringSets are unbounded, mutable sets of // Strings. A typical StringSet is { x1, ..., xn } public StringSet () // EFFECTS: Initializes this to be empty: { } public void insert (String s) // MODIFIES: this // EFFECTS: Adds x to the elements of this: // this_post = this_pre U { s } public boolean isIn (String s) { // EFFECTS: Returns true iff s is an element of this. public int size () // EFFECTS: Returns the number of elements in this. CS 201J Fall 2002

  14. Components of Data Abstractions • Ways to create objects of the data type • Creators: create new objects of the ADT from parameters of other types • Producers: create new objects of the ADT from parameters of the ADT type (and other types) • Ways to observe properties: observers • Ways to change properties: mutators CS 201J Fall 2002

  15. StringSet Operations • Creators StringSet () • Producers none • Observers isIn, size • Mutators insert public class StringSet // OVERVIEW: StringSets are unbounded, mutable sets of // Strings. A typical StringSet is { x1, ..., xn } public StringSet () // EFFECTS: Initializes this to be empty: { } public void insert (String s) // MODIFIES: this // EFFECTS: Adds x to the elements of this: // this_post = this_pre U { s } public boolean isIn (String s) { // EFFECTS: Returns true iff s is an element of this. public int size () // EFFECTS: Returns the number of elements in this. CS 201J Fall 2002

  16. Using Abstract Data Types • PS1, PS2 • Client interacts with data type using the methods as described in the specification • Client does not know the concrete representation CS 201J Fall 2002

  17. Implementing Abstract Data Types CS 201J Fall 2002

  18. Choosing a Representation • Need a concrete data representation to store the state • Think about how methods will be implemented • A good representation choice should: • Enable easy implementations of all methods • Allow performance-critical methods to be implemented efficiently CS 201J Fall 2002

  19. StringSet Representation • Option 1: private String [] rep; • Recall Java arrays are bounded • Easy to implement most methods, hard to implement insert • Option 2: private Vector rep; • Easy to implement all methods • Performance may be worse than for array CS 201J Fall 2002

  20. Implementing StringSet public class StringSet { // OVERVIEW: StringSets are unbounded, mutable sets of Strings. // A typical StringSet is {x1, ..., xn} // Representation: private Vector rep; public StringSet () { // EFFECTS: Initializes this to be empty: { } rep = new Vector (); } public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } rep.add (s); } Could this implementation of insert be correct? CS 201J Fall 2002

  21. It depends… public int size () { // EFFECTS: Returns the number of elements in this. StringSet uniqueels = new StringSet (); for (int i = 0; i < rep.size (); i++) { String current = (String) rep.elementAt (i); if (uniqueels.isIn (current)) { ; } else { uniqueels.insert (current); } } return uniqueels.rep.size (); } CS 201J Fall 2002

  22. Is it correct? public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); } public void insert (String s) { if (!isIn (s)) rep.add (s); } CS 201J Fall 2002

  23. Reasoning About Data Abstractions • How can we possibly implement data abstractions correctly if correctness of one method depends on how other methods are implemented? • How can we possibly test a data abstraction implementation if there are complex interdependencies between methods? CS 201J Fall 2002

  24. What must we know to know size is correct? • This implementation is correct only if we know the rep does not contain duplicates public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); } CS 201J Fall 2002

  25. Rep Invariant • The Representation Invariant expresses properties all legitimate objects of the ADT must satisfy I: C→ boolean Function from concrete representation to boolean. • Helps us reason about correctness of methods independently CS 201J Fall 2002

  26. Reasoning with Rep Invariants • Prove all objects satisfy the invariant before leaving the implementation code • Assume all objects passed in satisfy the invariant REQUIRES: Rep Invariant is true for this (and any other reachable ADT objects) EFFECTS: Rep Invariant is true for all new and modified ADT object on exit. CS 201J Fall 2002

  27. Rep Invariant for StringSet public class StringSet { // OVERVIEW: StringSets are unbounded, // mutable sets of Strings. // A typical StringSet is {x1, ..., xn} // Representation: private Vector rep; // RepInvariant (c) = // c contains no duplicates // && c != null CS 201J Fall 2002

  28. Implementing Insert? public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } rep.add (s); } Not a correct implementation: after it returns this might not satisfy the rep invariant! CS 201J Fall 2002

  29. Implementing Insert public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!isIn (s)) { rep.add (s); } } Possibly correct implementation: we need to know how to map rep to abstraction notation to know if this_post = this_pre U { s } CS 201J Fall 2002

  30. Abstraction Function • The Abstraction Function maps a concrete state to an abstract state: AF: C→ A Function from concrete representation to the abstract notation introduced in overview specification. • Range is concrete states for which RI is true CS 201J Fall 2002

  31. Abstraction Function for StringSet public class StringSet { // OVERVIEW: StringSets are unbounded, // mutable sets of Strings. // A typical StringSet is {x1, ..., xn} // Representation: private Vector rep; // AF (c) = // { AFString (c.els[i]) | 0 <= i < c.els.size () } CS 201J Fall 2002

  32. Correctness of Insert public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!isIn (s)) { rep.add (s); } } Use abstraction function to show if add implements its specification, the AF(rep_post) = AF(rep_pre) U {AFString(s)} CS 201J Fall 2002

  33. Reality Check • Writing abstraction functions, rep invariants, testing code thoroughly, reasoning about correctness, etc. for a big program is a ridiculous amount of work! • Does anyone really do this? • Yes (and a lot more), but usually only when its really important to get things right: • Cost per line of code: • Small, unimportant projects: $1-5/line • WindowsNT: about $100/line • FAA’s Automation System (1982-1994): $900/line CS 201J Fall 2002

  34. Charge • PS3: due next Thursday • Implement the StringTable abstract data type you used in PS2 • Longer than PS2 • Reason about data types using abstraction functions and rep invariants • Thursday: ESC/Java annotations and rep invariants • Tuesday: Practice with data abstractions CS 201J Fall 2002

More Related