Abstraction in Everyday Life Abstraction in Computer Science

What is Abstraction?

¡ô It's what you get when you¡­

¡ô

¡ô

¡ô The canonical Black Box

¡ô The Platonic ideal

¡ô The foundation of Computer Science

Abstraction and Modules

[Chapters 1 & 2]

¡ô

7/8/98

CSE 143 Summer 1998

Ignore the messy details

Focus on the essential qualities

Every item in your bag of tricks is an abstraction

7/8/98

64

CSE 143 Summer 1998

65

Abstraction in Everyday Life

Abstraction in Computer Science

¡ô We intuitively make and use abstractions

¡ô They provide us with mental models for the

world around us

¡ô They make us smarter

¡ô Programming languages contain abstraction

mechanisms

¡ô

¡ô

¡ô

¡ô

¡ô

A tool for building a new abstraction

Examples: functions, classes, modules

¡ô Two components: specification and

implementation

Better organization of information

Better ability to cope with complexity

Better capacity for problem-solving

¡ô

¡ô

Specification: what an abstraction promises to do

Implementation: how it keeps that promise

¡ô Once implementation works, forget about it!

7/8/98

CSE 143 Summer 1998

66

Example

7/8/98

67

Choosing the Right Abstraction

¡ô Often, many abstractions are possible

¡ô Trick is to choose the right one

¡ô Correct abstraction depends on what¡¯s

¡°essential¡±

food

Dog

waste material

7/8/98

CSE 143 Summer 1998

CSE 143 Summer 1998

68

7/8/98

shoes

food

Dog

Dog

garbage

loyalty affection

CSE 143 Summer 1998

69

1

Levels of Abstraction

Layers of Abstraction

¡ô A higher level of abstraction disregards more

details

¡ô A dog is¡­

¡ô An abstraction can be composed out of other

abstractions

Dog

¡ô

¡ô

¡ô

¡ô

¡ô

¡ô Must choose the correct level of abstraction

¡ô

¡ô

Black boxes within black boxes

¡ô Layers don¡¯t know internals

of lower layers

A physical system (a collection of atoms)

A bag of organs

A furry thing that slobbers

Skeleton

Kidney

No crossing of the

¡°Abstraction Barrier¡±

Heart

Ventricle

Chamber

Too low: too many messy details still remain

Too high: difficult to understand/control behaviour

7/8/98

CSE 143 Summer 1998

70

7/8/98

CSE 143 Summer 1998

Example

Program Decomposition

¡ô OSI networking model: seven layers

¡ô When designing a program, the first step is to

break it down into manageable chunks

application

¡ô

presentation

session

¡ô

network

link

physical

CSE 143 Summer 1998

And repeat the process for each chunk!

Some chunks are given to you, e.g. a library

¡ô Implement the chunks, ¡°glue together¡± for final

program

¡ô Important to find correct layers and levels

¡ô Not all program design is top-down in this way

transport

7/8/98

72

7/8/98

CSE 143 Summer 1998

The Chunk Hierarchy

Modules

¡ô There¡¯s no fixed set of names for layers of

decomposition

¡ô But many, many buzzwords

¡ô C and C++ do not require multiple files

¡ô

71

73

Many ideas about how this should be done

¡ô function, class, module, component, library,

toolkit, framework, subsystem, system, ¡­

¡ô In this course, we¡¯ll focus on the first three

¡ô Guh.

7/8/98

CSE 143 Summer 1998

74

7/8/98

CSE 143 Summer 1998

75

2

What is a Module?

Modularization

¡ô A unit of decomposition

¡ô A unit of reusability

¡ô A collection of related items packaged together

¡ô Example: a stereo system

¡ô Basic idea: break apart large system into

smaller units (modules)

¡ô Group related functionality in one module

¡ô Design modules to be general and reusable

¡ô

¡ô

Multiple times in same program

Different programs/programmers

¡ô Package modules into black boxes,

communicate via interfaces

7/8/98

CSE 143 Summer 1998

76

7/8/98

CSE 143 Summer 1998

Specification as Contract

Locality

¡ô Module specification acts as a contract between

client and implementor

¡ô Client depends on specification not changing

¡ô Doesn¡¯t need to know any details of how module

works, just what it does

¡ô Implementor can change anything not in the

specification, (eg. to improve performance)

¡ô Locality of design decisions from encapsulation

¡ô Benefits of private data and algorithm locality:

7/8/98

7/8/98

CSE 143 Summer 1998

78

¡ô

¡ô

¡ô

¡ô

Division of labour

Easier to understand

Implementation independence

Platform independence

CSE 143 Summer 1998

Modules in C++

Imports and Exports

¡ô Modules represented by a pair of files

¡ô Specification (.h) file declares which items are

exported

¡ô

¡ô

specification (.h) file

implementation (.cpp, .cc, .c++, .C, etc) file

¡ô

¡ô

¡ô

CSE 143 Summer 1998

79

constants, function prototypes, and data types

¡ô Client program must import features of a module

to use them

¡ô Client¡¯s only interaction with module is through

the interface defined in the .h file

7/8/98

77

80

7/8/98

Use the #include directive

Implementation (.cpp) file also uses #include to

ensure it obeys the contract

CSE 143 Summer 1998

81

3

Specification

Sample Specification File

¡ô Supplies constants, data types, function

prototypes

¡ô Comments describing what each function does

// geometry.h -- Specification file for

// computational geometry functions

¡ô

#ifndef __GEOMETRY_H__

prevent

preventmultiple

multipleinclusion!

inclusion!

#define __GEOMETRY_H__

Including preconditions, postconditions and

invariants, as appropriate

// circleArea: Returns the area of a circle with given radius

prototype

double circleArea( double radius );

¡ô Client should be able to refer to specification as

module¡¯s documentation

// circleRadius: Returns the radius of a circle of given area

// PRE: area must be non-negative

double circleRadius( double area );

#endif // __GEOMETRY_H__

7/8/98

CSE 143 Summer 1998

82

Sample Implementation File

7/8/98

geometry.h

CSE 143 Summer 1998

83

Sample Client File

// geometry.cpp

// Implementation of geometry functions

#include

#include "geometry.h"

#include

#include "geometry.h"

int main( void )

{

double value;

const double PI = 3.1415;

double circleArea( double radius ) {

return PI * radius * radius;

}

double circleRadius ( double area ) {

return sqrt( area / PI );

}

7/8/98

cout > value;

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download