Lecture 7: Data Abstractions

Similar documents
What is an algorithm?

What is an algorithm?

Lecture 5 Abstract Data Types

Representation Invariants and Abstraction Functions

JAVA MOCK TEST JAVA MOCK TEST II

CSI33 Data Structures

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CSE 331 Software Design & Implementation

Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

CSE 331 Software Design & Implementation

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

Lecture 12: Abstraction Functions

6.005 Elements of Software Construction Fall 2008

CSE 331 Software Design & Implementation

Announcements/Follow-ups

6.170 Lecture 7 Abstract Data Types MIT EECS

Lecture 5 Representation Invariants

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

CS112 Lecture: Defining Instantiable Classes

CS 617 Object Oriented Systems Lecture 3 Object Abstractions, Encapsulation, Abstract Data Types 3:30-5:00pm Thu, Jan 10

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Subclassing for ADTs Implementation

Designing Robust Classes

Dot and Scope Resolution Operator

Assertions. Assertions - Example

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Concepts of Programming Languages

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Chapter 1: Object-Oriented Programming Using C++

Introduction to Programming Using Java (98-388)

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Lecture 7 Abstraction Functions

CS Computable Functions. Reading: Chapter 2

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Classes Classes 2 / 35

Chapter 4 Defining Classes I

JAVA: A Primer. By: Amrita Rajagopal

Object Orientation. Chapter Sixteen Modern Programming Languages, 2nd ed. 1

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Programming Languages

Lecture Chapter 2 Software Development

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Classes, interfaces, & documentation. Review of basic building blocks

CSE 230 Computer Science II (Data Structure) Introduction

Top Down Design vs. Modularization

Advanced JML Erik Poll Radboud University Nijmegen

Introduction to Object- Oriented Programming

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

CMPT Data and Program Organization

Sec$on 2: Specifica)on, ADTs, RI WITH MATERIAL FROM MANY

TYPES, VALUES AND DECLARATIONS

Solutions to Quiz 1 (October 19, 2015)

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

Representation Invariant, Abstraction Function

Data Structures and Algorithms. Chapter 1

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

CGS 2405 Advanced Programming with C++ Course Justification

Java Object Oriented Design. CSC207 Fall 2014

CS2210 Data Structures and Algorithms

Understanding an ADT implementation: Abstraction functions

Data Abstraction: The Walls

Principles of Programming Languages

STUDENT LESSON A5 Designing and Using Classes

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Representation invariants and abstraction functions CSE 331 UW CSE

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

G Programming Languages - Fall 2012

September 10,

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

INTERFACES IN JAVA. Prof. Chris Jermaine

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

#include <iostream> #include <cstdlib>

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Object Orientated Analysis and Design. Benjamin Kenwright

write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Programmiersprachen (Programming Languages)

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Hash table basics. ate à. à à mod à 83

ECE 122. Engineering Problem Solving with Java

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Chapter 5 Names, Binding, Type Checking and Scopes

OOPLs Semantic Foundations. Data Abstraction Foundation of OOPLs

Classes Classes 2 / 36

COURSE 2 DESIGN PATTERNS

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Transcription:

Lecture 7: Data Abstractions Abstract Data Types Data Abstractions How to define them Implementation issues Abstraction functions and invariants Adequacy (and some requirements analysis) Towards Object Orientation differences between object oriented programming and data abstraction CSC444 Lec07 1

Imagine we want to hold information about dates E.g. year, month, day, hours, minutes, seconds, day of week, etc. Could use an integer arrays: int date [3]; and write some support functions for computing day of week, comparing dates, But suppose we then decide years need 4 digits rather than 2 (i.e. 2001 instead of 01) we have to change every part of the program that uses dates Encapsulation Example e.g. if we used arrays of int for date & time: int int today[3]; today[3]; int int lecture1_time[3]; lecture1_time[3]; today[0] today[0] = 01; 01; today[1] today[1] = 10; 10; today[2] today[2] = 01; 01; lecture1_time[0] lecture1_time[0] = 9; 9; lecture1_time[1] lecture1_time[1] = 0; 0; lecture1_time[2] lecture1_time[2] = 0; 0; distinguish the abstract notion of a date from it s concrete representation hide all the details about how dates are represented Benefits: modifiability, testability, readability, reduced complexity, [Y2K compliance(!?)] CSC444 Lec07 2

Programming languages provide: Some concrete data types integers, characters, arrays, Some abstract data types floating point, lists, tables, two dimensional arrays, records, Abstract Data Types (ADTs) Abstract data types are implemented using concrete data types (but you don t need to know this to use them) Operations are provided for each datatype e.g. creation, assignment, etc. but you cannot muck around with the internal representations e.g. float is represented in two parts, but you cannot access these directly But: some languages do allow you access to the internal representations e.g. in C, you can use pointers to access the internals of arrays this removes the distinction between the abstraction and the implementation it destroys most of the benefits of abstraction it causes confusion and error CSC444 Lec07 3

Source: Liskov & Guttag 2000, p77-78 Home-made abstract data types Encapsulation is improved if you create your own data abstractions choice of what abstractions to create depends on the application Application Useful data abstractions Compiler writing tables, stacks, Banking accounts, customers, Mathematical computing matrices, sets, polynomials, Graph Editing graphs, nodes, edges, positions choice of operations depends on how you want to manipulate the data e.g. bank accounts: open, close, make a deposit, make a withdrawal, check the balance, e.g. graphs: initialize, add nodes, remove nodes, check whether there is an edge between two nodes, get the label for a node, Most languages support creation of new datatypes but they might not force you to specify the data abstraction and they might not enforce information hiding CSC444 Lec07 4

Source: Liskov & Guttag 2000, p117-118 Operations on Data Abstractions Four groups of operators: Creators create new objects of the datatype Producers Mutators take existing objects of the datatype and build new ones modify existing objects of the datatype Observers tell you information about existing objects of the datatype (without changing them) Immutable datatypes don t have mutators they can be created and destroyed, but not modified once you ve created an object you cannot change it Example: Example: sets sets Creators: Creators: create create a new new empty empty set, set, Producers: Producers: set set union, union, set set intersection, intersection, mutators: mutators: add add an an element, element, remove remove an an element, element, observers: observers: set set size, size, set set membership, membership, set set equality, equality, test test for for empty empty set, set, CSC444 Lec07 5

Source: Liskov & Guttag 2000, section 5.1 Defining Data Abstractions The abstraction should: name the data type list its operations describe the data abstraction in English say whether it s mutable or not give a procedural abstraction for each operation the abstraction only lists the public operations there may be other private procedures hidden inside /* /* datatype datatype set set has has operators operators create, create, insert, insert, delete, delete, member, member, size, size, union, union, intersection. intersection. overview: overview: sets sets are are unbounded unbounded mathematical mathematical sets sets of of integers. integers. They They are are mutable: mutable: insert insert and and delete delete are are the the mutation operations. mutation operations. operations: operations: procedure procedure create create () () returns returns set set effects: effects: x is is a new new empty empty set set procedure procedure insert insert (set (set s, s, int int x) x) returns returns null null effects: effects: adds adds x to to the the set set s s such such that that s s = s s» {x} {x} procedure procedure delete delete (set (set s, s, int int x) x) returns returns null null requires: requires: x Œ s s effects: effects: s s = s s - {x} {x} (etc) (etc) */ */ CSC444 Lec07 6

Java Example public public class class IntSet IntSet { //Overview: //Overview: IntSets IntSets are are mutable, mutable, unbounded unbounded sets sets of of integers. integers. A typical typical IntSet IntSet is is {x {x 1, 1 x x n } n //Creators //Creators public public IntSet IntSet () () //effects: //effects: Initializes Initializes this this to to be be the the empty empty set set //Mutators //Mutators public public void void insert insert (int (int x) x) //effects: adds x to the set this such that this = this» {x} //effects: adds x to the set this such that this = this» {x} public public void void delete delete (int (int x) x) //requires: //requires: x Œ this this //effects: //effects: this this = this this - {x} {x} //Observers //Observers public public boolean boolean member member (int (int x) x) //effects: //effects: returns returns true true if if x Œ this, this, false false otherwise otherwise //Producers //Producers public public IntSet IntSet intersection intersection (IntSet (IntSet a) a) //effects: //effects: returns returns a new new set set representing representing a «this this } Source: adapted from Liskov & Guttag 2000, p81 CSC444 Lec07 7

Source: Liskov & Guttag 2000, section 5.3 Implementing Data Abstractions Choose a representation that: permits all operations to be implemented easily (and reasonably efficiently) permits frequent operations to run faster Example: sets an unsorted array with repeated elements insert is very fast, union is fast, intersection and member are slow, delete is very slow a sorted array insert is very slow, member is very fast, intersection is fast, union is slow a linked list insert is fast, delete is fast, union is slow, takes more memory Choose a programming mechanism Package hides private code, package has to be imported (e.g. Ada, C, Modula) Object provides inheritance, operations called by message passing (e.g. C++, Java) Abstract datatype provides strong type checking, object becomes part of the language (e.g. C, ML) CSC444 Lec07 8

Source: Liskov & Guttag 2000, p99-100 Abstraction vs. Implementation There is a mapping between abstract objects and their representations several rep objects might map to the same abstraction object some rep objects might be invalid every abstract object must have a rep object { 1, 2, 3 } { } abstract objects { 7 } invalid rep objects 1 1 7 7 2 3 7 3 2 CSC444 Lec07 9

A data abstraction is adequate if Adequacy Source: Liskov & Guttag 2000, p118-9 it provides all the operations the users (e.g. other programmers!) will need Choices, choices, choices e.g. for sets, member(s,x) isn t strictly necessary: could do intersection(s,create_set(x)) and test if the result is empty could do delete(s,x) and see if we get an error message but member(s,x) is much more convenient. Such choices affect functionality, convenience & efficiency functionality: make sure all required operations are possible convenience: make sure that typical/frequent operations are simple to use efficiency: make frequent operations cheaper (usually by choosing an appropriate rep type - this should not affect the choice of abstraction) Some requirements analysis is needed What data objects will be needed? What operations will need to be performed on them? What usage patterns are typical? use cases / scenarios are helpful here CSC444 Lec07 10

Source: van der Linden, 1996, chp2, and Blum, 1992, pp313-329 Object Orientation Object Orientation extends data abstraction Data abstraction becomes the main structuring mechanism for programs No fixed control structure Object Oriented programming languages have: Abstraction Encapsulation - methods and objects are bundled together Polymorphism - same name can be used for different objects methods Dynamic binding - don t know which method/object is referred to until runtime Inheritance - can extend existing data abstractions to create new ones Use OO design principles in any programming language Write data abstractions for all complex data structures Hide the implementations using ADTs or packages Only access the data abstractions through their defined operations (ª methods ) Some OOP mechanisms are less important Polymorphism & dynamic binding are not relevant at the design level (these are programming tricks that make programs more complex) Inheritance can be done manually CSC444 Lec07 11

Summary Data Abstractions lead to good program design They help with encapsulation (information hiding) They help reduce the complexity of software interfaces They make programs more modifiable Need some analysis to choose good data abstractions Adequacy: have you included all the operations that users need can switch between implementations to improve efficiency Data abstraction abstract data types ADTs are one way to implement data abstraction can also use packages, objects, Data abstraction object-oriented programming data abstraction is really a design technique (the basis of OOD) can use it in any programming language some programming languages provide more support than others CSC444 Lec07 12

References Liskov, B. and Guttag, J., Program Development in Java: Abstraction, Specification and Object-Oriented Design, 2000, Addison-Wesley. ƒ Chapter 5 provides a thorough coverage of data abstractions. Blum, B. Software Engineering: A Holistic View. Oxford University Press, 1992 ƒ see especially section 4.2 for comments on data abstraction and object oriented design. (historical note: Java is conspicuously absent from Blum s list of object oriented languages. The technology has changed dramatically in eight years! However, the principles are the same) van der Linden, P. Just Java. 1996, Sunsoft Press ƒ A rare book on object oriented programming in Java written by someone that can explain it properly. van Vliet, H. Software Engineering: Principles and Practice (2nd Edition) Wiley, 1999. ƒ mentions data abstraction only in passing in section 11.1. Chapter 15 gives a much more formal coverage of specifying data abstractions via algebraic specs (15.3), and via formal pre- and postconditions (15.4). This is more formal than I expect to use on this course, but worth a read to see where some of the ideas came from. CSC444 Lec07 13