cs205: engineering softwareuniversity of virginia fall 2006DataAbstractionDavid Evanswww.cs.virginia.edu/cs205
2cs205: engineering softwareManaging Complexity• Modularity– Divided problem into procedures– Used specifications to separ...
3cs205: engineering softwareData Abstraction• We need new data types, not justprocedures– Could you implement PS2 without ...
4cs205: engineering softwareAbstract Data Types• Separate what you can do with datafrom how it is represented• Client inte...
5cs205: engineering softwareData Abstraction in Java• A class defines a new data type• Use private instance variables tohi...
6cs205: engineering softwareUp and DownAbstract TypeConcrete Representationclass implementationclientsdownupThe representa...
7cs205: engineering softwareExample: CellStatepublic class CellState {// OVERVIEW: A CellState is an immutable// object th...
8cs205: engineering softwareCell State Representationprivate boolean alive;public boolean isAlive () { return alive; }Abst...
9cs205: engineering softwareAdvantages/Disadvantages- More code to write and maintain- Run-time overhead (time to callmeth...
10cs205: engineering softwareSet Example (ps2)• Set abstract data type: represent aset of objects• Operations:– Create an ...
11cs205: engineering softwareType Parameters• We want to have sets of differenttypes of objects• How should we declare the...
12cs205: engineering softwareGeneric Datatypepublic class Set<T> {...public boolean add(T el)public T choose()public boole...
13cs205: engineering softwareCreating Specific Typespublic class Set<T> {...public boolean add(T el)public T choose()publi...
14cs205: engineering softwareAbstract Data Type Specifications• Overview: what the type represents– Mutability/Immutabilit...
15cs205: engineering softwareSet Specificationpublic class Set<T> implements Iterable<T>, Collection<T> {OVERVIEW: A Set i...
16cs205: engineering softwareComponents of Data Abstractions• Ways to create objects of the data type– Creators: create ne...
17cs205: engineering softwareSet Operationspublic class Set<T> implements Iterable<T>, Collection<T> {OVERVIEW: A Set is a...
18cs205: engineering softwarePS2• Several datatype implementations providedwith specifications• Client interacts with data...
Upcoming SlideShare
Loading in …5
×

Lecture7

175 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
175
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture7

  1. 1. cs205: engineering softwareuniversity of virginia fall 2006DataAbstractionDavid Evanswww.cs.virginia.edu/cs205
  2. 2. 2cs205: engineering softwareManaging Complexity• Modularity– Divided problem into procedures– Used specifications to separate whatfrom how• A big program can have thousands ofprocedures
  3. 3. 3cs205: engineering softwareData Abstraction• We need new data types, not justprocedures– Could you implement PS2 without theDirectedGraph type?– We could make procedures, but whatwould you pass to them?• Organize program around abstractdata types
  4. 4. 4cs205: engineering softwareAbstract Data Types• Separate what you can do with datafrom how it is represented• Client interacts with data throughprovided operations according totheir specifications• Implementation chooses how torepresent data and implement itsoperations
  5. 5. 5cs205: engineering softwareData Abstraction in Java• A class defines a new data type• Use private instance variables tohide the choice of representation– private declarations are only visibleinside the class
  6. 6. 6cs205: engineering softwareUp and DownAbstract TypeConcrete Representationclass implementationclientsdownupThe representation of an abstract data typeis visible only in the class implementation.Clients manipulate an abstract data type bycalling its operations (methods and constructors)
  7. 7. 7cs205: engineering softwareExample: CellStatepublic 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.}
  8. 8. 8cs205: engineering softwareCell State Representationprivate boolean alive;public boolean isAlive () { return alive; }Abstract TypeConcrete Representationclass implementationclientsdownupCellState cs.isAlive ()
  9. 9. 9cs205: engineering softwareAdvantages/Disadvantages- More code to write and maintain- Run-time overhead (time to callmethod)+ Client doesn’t need to know aboutrepresentation+ Suppose we want to add more states(e.g., question 2)
  10. 10. 10cs205: engineering softwareSet Example (ps2)• Set abstract data type: represent aset of objects• Operations:– Create an empty set– Mathematical set operations: add,contains, size, remove, union
  11. 11. 11cs205: engineering softwareType Parameters• We want to have sets of differenttypes of objects• How should we declare the Setmethods?public boolean add(?? elem)public boolean contains(?? elem)public ?? choose()We don’t want just one Set datatype.We want different Sets for different element types.
  12. 12. 12cs205: engineering softwareGeneric Datatypepublic class Set<T> {...public boolean add(T el)public T choose()public boolean contains(T el)...} Note: Java did not support genericdatatypes until version 1.5 (this is whythe book doesn’t use them)
  13. 13. 13cs205: engineering softwareCreating Specific Typespublic class Set<T> {...public boolean add(T el)public T choose()public boolean contains(T el)...}public class Set<String> {...public boolean add(String el)public String choose()public boolean contains(String el)...}Set<String>
  14. 14. 14cs205: engineering softwareAbstract Data Type Specifications• Overview: what the type represents– Mutability/ImmutabilityA Set is a mutable, unbounded set of objectsof type T.– Abstract NotationA typical Set is { x1, …, xn }.• Operations: procedural specificationsfor each operation (public methods andconstructors); use the abstractnotation introduced in overview.
  15. 15. 15cs205: engineering softwareSet Specificationpublic class Set<T> implements Iterable<T>, Collection<T> {OVERVIEW: A Set is a mutable, unbounded set of objects oftype T. A typical Set is {x_1, ..., x_n }.public Set()EFFECTS: Initializes this to an empty set: { }.public boolean add(T el)MODIFIES: thisEFFECTS: Adds el to the elements of this:thispost = thispre U { el }Returns true iff el was not an element of thispre.
  16. 16. 16cs205: engineering softwareComponents of Data Abstractions• Ways to create objects of the data type– Creators: create new objects of the ADTfrom parameters of other types– Producers: create new objects of the ADTfrom parameters of the ADT type (andother types)• Ways to observe properties: observers• Ways to change properties: mutators
  17. 17. 17cs205: engineering softwareSet Operationspublic class Set<T> implements Iterable<T>, Collection<T> {OVERVIEW: A Set is a mutable, unbounded set of objects oftype T. A typical Set is {x_1, ..., x_n }.public Set()EFFECTS: Initializes this to an empty set: { }.public Set(Set<T> s)EFFECTS: Initializes this to a set containing thesame elements as the set s (a shallow copy).public boolean add(T el)MODIFIES: thisEFFECTS: Adds el to the elements of this:this_post = this_pre U { el }Returns true iff el was not an element of this_pre.public void union(Set<T> t)MODIFIES: thisEFFECTS: this_post = this_pre U tpublic T choose()REQUIRES: this has at least one elementEFFECTS: Returns an element of this.public boolean contains(Object el)EFFECTS: Returns true iff el is an element of this.public int size()EFFECTS: Returns the number of elements in this.public boolean isEmpty()EFFECTS: Returns true iff this has no elements.public boolean remove(Object el)MODIFIES: thisEFFECTS: Removes el from this:this_post = this_pre - { el }Returns true iff el is in this_pre
  18. 18. 18cs205: engineering softwarePS2• Several datatype implementations providedwith specifications• Client interacts with data type using themethods as described in the specification• Client does not know the concreterepresentation• Your code should work correctly with anycorrect implementation of the specifieddatatype

×