Data Abstraction: CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul Nate@virginia - Edu
Data Abstraction: CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul Nate@virginia - Edu
• Abstraction helps
– Many-to-one – “forget the details”
– Must separate “what” from “how”
Information Hiding
• Modularity - Procedural abstraction
– By specification
• Locality
• Modifiability
– By parameterization
• Data Abstraction
– What you can do with the data is separated
from how it is represented
Software development cycle
• Specifications – What do you want to do?
• Design – How will you do what you want?
• Implement – Code it.
• Test – Check if it works.
• Maintain – School projects don’t usually
make it this far.
What is email
address of
nate?
Server
Database
Client
Client/Server/Database Interaction
I need
Nate’s
email.
Server
The interaction
between the server
and database is
Database your part.
Client
Client/Server/Database Interaction
Server
Database
Client
Client/Server/Database Interaction
[email protected]
Server
Database
Client
Example: Database System
• Need a new data type
• Abstract Data Types (ADTs)
– Help separate what from how
– Client will use the specifications for interaction
with data
– Client of the web database should not know
the “guts” of the implementation
Data abstraction in Java
• An ADT is defined by a class
– The ADT in the web/database application will
be a User
– A private instance variable hides the class
internals
– public String getEmail ();
• What is private in the implementation?
• OVERVIEW, EFFECTS, MODIFIES
– A class does not provide data abstraction by
itself
Class User { Accessibility
// OVERVIEW:
/* Client code using a
// mutable object User object, myUser */
// where the User
String nateEmail =
// is a library myUser.email;
// member. sendEmail(nateEmail);
public String email;
… /* The client’s code can
only see what is made
}
public in the User class.
The user’s email data is
public in the User class.
This is BAD. */
Program Maintenance
• Suppose storage space is at a premium
– Everyone in the database is
[email protected], so we can drop the
virginia.edu
[email protected] nate
– What kind of problems will occur with the code
just seen?
Program Maintenance
• Suppose storage space is at a premium
– Everyone in the database is
[email protected], so we can drop the
virginia.edu
[email protected] nate
– What kind of problems could occur had the
client code been able to access the email
address directly?
Email was
public in String nateEmail =
User class. myUser.email; ***ERROR!!!***
sendEmail(nateEmail);
Class User {
Accessibility (fixed)
// OVERVIEW: A
// mutable object where
// User is a library // Client code using a
// member. User object, myUser
getEmail() {
// EFFECTS: returns user’s /* This code properly
uses data abstraction
// primary email when returning the full
email address. */
return email;
}
Class User {
Accessibility (fixed)
// OVERVIEW: A
// mutable object where
// User is a library // Client code using a
// member. User object, myUser
String nateEmail =
private String email; myUser.getEmail();
… sendEmail(nateEmail);
public String
getEmail() { /* The database
// EFFECTS: returns user’s dropped the
@virginia.edu, and only
// primary email one line of code needed
changing. */
return email +“@virginia.edu”;
Advantages/Disadvantages of
Data Abstraction?
- More code to write and maintain initially
- Overhead of calling a method
- Greater initial time investment
Head
latePeople
User 1 User 2 …
List implementation
class GroupUsers {
// OVERVIEW:
// Mutable, unbounded group of users
User 1
X
User 2 User 3 X
Head
User 1 User 3 X
deleteUser(String userID)
Node.java
public void delete (String userID) {
// MODIFIES: this
// EFFECTS: this_pre = this_pre – node
// where node.userID = userID
Node currNode;
Node prevNode;
if(this.next == null) return;
prevNode = this;
currNode = this.next;