Docsity

Data Abstraction - Engineering Software | CS 2220, Study notes of Software Engineering

Toggle sidebar
bg1
1
cs205: engineering software
university of virginia fall 2006
Data
Abstraction
David Evans
www.cs.virginia.edu/cs205
2
cs205: engineering software
Managing Complexity
• Modularity
– Divided problem into procedures
– Used specifications to separate what
from how
• A big program can have thousands of
procedures
– How can we group them into modules?
3
cs205: engineering software
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
4
cs205: engineering software
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
5
cs205: engineering software
Up and Down
Abstract Type
Concrete Representation
class implementation
clients
down
up
The representation of an abstract data type
is visible only in the class implementation.
Clients manipulate an abstract data type by
calling its operations (methods and constructors)
6
cs205: engineering software
Cell State Representation
private boolean alive;
public boolean isAlive () { return alive; }
Abstract Type
Concrete Representation
class implementation
clients
down
up
CellState cs.isAlive ()
pf3
pf4
pf5

Partial preview of the text

Download Data Abstraction - Engineering Software | CS 2220 and more Study notes Software Engineering in PDF only on Docsity!

cs205: engineering software university of virginia fall 2006

Data

Abstraction

David Evans www.cs.virginia.edu/cs205 (^) cs205: engineering software 2

Managing Complexity

  • Modularity
    • Divided problem into procedures
    • Used specifications to separate what from how
  • A big program can have thousands of procedures - How can we group them into modules?

cs205: engineering software 3

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

cs205: engineering software 4

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

cs205: engineering software 5

Up and Down

Abstract Type Concrete Representation class implementation

clients down up

The representation of an abstract data type is visible only in the class implementation.

Clients manipulate an abstract data type by calling its operations (methods and constructors)

cs205: engineering software 6

Cell State Representation

private boolean alive; public boolean isAlive () { return alive; }

Abstract Type Concrete Representation class implementation

clients down up

CellState cs.isAlive ()

cs205: engineering software 7

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., question 2)

cs205: engineering software 8

Set Example (ps2)

  • Set abstract data type: represent a set of objects
  • Operations:
    • Create an empty set
    • Mathematical set operations: add, contains, size, remove, union

cs205: engineering software 9

Type Parameters

  • We want to have sets of different types of objects
  • How should we declare the Set methods? 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. cs205: engineering software 10

Generic Datatype

public class Set { ... public boolean add(T el) public T choose() public boolean contains(T el) ... } (^) Note: Java did not support generic datatypes until version 1.5 (this is why the book doesn’t use them)

cs205: engineering software 11

Creating Specific Types

public class Set { ... public boolean add(T el) public T choose() public boolean contains(T el) ... } (^) public class Set { ... public boolean add(String el) public String choose() public boolean contains(String el) ... }

Set

cs205: engineering software 12

Abstract Data Type Specifications

  • Overview: what the type represents
    • Mutability/Immutability A Set is a mutable, unbounded set of objects of type T.
    • Abstract Notation A typical Set is { x 1 , …, xn }.
  • Operations: procedural specifications for each operation (public methods and constructors); use the abstract notation introduced in overview.

cs205: engineering software 19

Implementing Set

public class Set { // OVERVIEW: Sets are unbounded, mutable sets of elements of type T. // A typical Set 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?

cs205: engineering software 20

It depends…

public int size () { // EFFECTS: Returns the number of elements in this. Set unique = new Set (); for (T el : rep) { if (!unique.isIn (el)) { unique.add (current); } } return unique.rep.size (); }

cs205: engineering software 21

Is it correct?

public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); }

public void insert (String s) { if (!contains (s)) rep.add (s); }

cs205: engineering software 22

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?

cs205: engineering software 23

What must we know to know if 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 (); }

cs205: engineering software 24

To Reason about Operations

  • We need to know:
  • How the concrete rep maps to abstract values: Abstraction Function
  • What values of the concrete rep are valid: Representation Invariant

cs205: engineering software 25

Rep Invariant

  • Predicate that all legitimate objects of the ADT must satisfy I: C → boolean
  • Helps us reason about correctness of methods independently - Prove all objects satisfy the invariant before leaving the implementation code - Assume all objects passed in satisfy the invariant cs205: engineering software 26

Reasoning with Rep Invariants

REQUIRES: Rep Invariant is true for this (and any other reachable ADT objects)

EFFECTS: Rep Invariant is true for all new and modified ADT objects on exit.

Every public datatype operation implicitly includes these preconditions and postconditions.

cs205: engineering software 27

Rep Invariant for Set

public class Set { // Representation: private Vector rep;

// RepInvariant (c) = c contains no duplicates

or // RepInvariant (c) = // forall i, j: rep[i].equals(rep[j]) // only when i == j

cs205: engineering software 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!

cs205: engineering software 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 (!contains (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 } cs205: engineering software 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. What is the range of the Abstraction Function? Range is concrete states for which rep invariant is true