data abstraction

18
Data Abstraction Chapter 9

Upload: ellie

Post on 21-Jan-2016

57 views

Category:

Documents


0 download

DESCRIPTION

Data Abstraction. Chapter 9. Outline. Concept of abstraction Abstract data types with example Encapsulation with example. The Concept of Abstraction. Say what a program does without necessarily saying how it does it - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data  Abstraction

Data AbstractionChapter 9

Page 2: Data  Abstraction

Concept of abstraction Abstract data types with example Encapsulation with example

Outline

Page 3: Data  Abstraction

Say what a program does without necessarily saying how it does it

A process of generalization, removing restrictions, eliminating detail, removing inessential information etc

“more potential implementations” – moving to a lower level mans restricting the number of potential implementations

The Concept of Abstraction

Page 4: Data  Abstraction

A data type is characterized by:◦ a set of values◦ a data representation, which is common to all

these values, and ◦ a set of operations, which can be applied

uniformly to all these values

Data types

Page 5: Data  Abstraction

C is not object-oriented, but we can still manage to inject some object-oriented principles into the design of C code.

For example, a data structure and its operations can be packaged together into an entity called an ADT.

The lower-level implementation details of the data structure are hidden from view of the rest of the program.

The implementation details can be changed without altering the ADT interface.

Abstract Data Types (ADTs)

Page 6: Data  Abstraction

Data abstraction + operation abstraction An abstract data type consists of:

◦ Data structure: stores information to represent a certain concept

◦ Functionality: set of operations that can be applied to the data type

Abstract Data Types (ADTs)

Page 7: Data  Abstraction

As an example the description of the ADT Integer is presented. Let k be an integer expression:

ADT Integer is ◦ Data

A sequence of digits optionally prefixed by a plus or minus sign. We refer to this signed whole number as N.

◦ Operations constructor Creates a new integer. add(k) Creates a new integer which is the sum of N and k.

Consequently, the postcondition of this operation is sum = N+k. Don't confuse this with assign statements as used in programming languages! It is rather a mathematical equation which yields ``true'' for each value sum, N and k after add has been performed.

sub(k) Similar to add, this operation creates a new integer of the difference of both integer values. Therefore the postcondition for this operation is sum = N-k.

set(k) Set N to k. The postcondition for this operation is N = k. ... end

ADT : Example

Page 8: Data  Abstraction

The entities define the data structure of a set of items. For example, each administered employee has a name, date of birth and social number.

The data structure can only be accessed with defined operations. This set of operations is called interface and is exported by the entity. An entity with the properties just described is called an abstract data type (ADT).

Properties of Abstract Data Types

Page 9: Data  Abstraction

Figure below shows an ADT which consists of an abstract data structure and operations. Only the operations are viewable from the outside and define the interface. Separate the use of a data structure from the details of its implementation. This is the principle underlying the use of abstract data types.

Here are some examples: stack: operations are "push an item onto the stack", "pop an item from the

stack", "ask if the stack is empty"; implementation may be as array or linked list or whatever.

queue: operations are "add to the end of the queue", "delete from the beginning of the queue", "ask if the queue is empty"; implementation may be as array or linked list or heap.

search structure: operations are "insert an item", "ask if an item is in the structure", and "delete an item"; implementation may be as array, linked list, tree, hash table, ...

Properties of Abstract Data Types (con’t)

Page 10: Data  Abstraction

To implement an ADT, you need to choose:◦ a data representation that

must be able to represent all possible values of the ADT should be private

◦ a set of methods that support normal use of the ADT The user must be able to create, possibly modify, and

examine the values of the ADT◦ an algorithm for each of the possible operations

that must be consistent with the chosen representation all auxiliary (helper) operations that are not in the

contract should be private

Implementing an ADT

Page 11: Data  Abstraction

Domain concepts are reflected in the code Encapsulation: internal complexity, data

and operation details are hidden Specification vs. implementation: usage of

data type is independent from its internal implementation

Higher modularity Increase ease of maintenance and reuse of

code

Abstract Data Types: Benefits

Page 12: Data  Abstraction

ADT : Programming example

Page 13: Data  Abstraction

ADT Extensibility

Page 14: Data  Abstraction

Focus on the external behavior of something and ignores the internal details of how the behavior is produced

Different part of the software to be effectively isolated in operation

Any component has one or more simple, well-defined external interfaces (defined in a standard)

Encapsulation

Page 15: Data  Abstraction

Encapsulation and Objects

Page 16: Data  Abstraction

Encapsulation : Object example

Optical and wireless

Optical and wiredWheeled and wireless

Page 17: Data  Abstraction

Encapsulation : Benefits

Page 18: Data  Abstraction

Abstraction Data Types◦ Data◦ Operations

Benefits of using ADT Encapsulation

◦ Implementation in C language◦ Objects◦ Advantages

Summary