Data Abstraction and Modules in Modula-2 and Ada, Papers of Programming Languages

Data abstraction, a programming methodology that supports modifiability, extensibility, and re-usability through encapsulation. The concept of data abstraction and its implementation in modula-2 and ada using modules. Modules are collections of declarations, including types, variables, and procedures, that can be imported and exported to create encapsulated namespaces. The roles of modules, their interface and implementation, and management issues.

Typology: Papers

Pre 2010

Uploaded on 07/30/2009

koofers-user-8ga-1
koofers-user-8ga-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Development of complex software systems
How do we deal with complexity of software development?
Miller: “People can keep track of seven things, be they bits,
words, colors, tones, or tastes. Tentatively, therefore, we are
justified in assuming that our memories are limited by the
number of units or symbols we must master, but not by the
amount of information that these symbols represent.”
Deal with complexity through decomposition,composition,
and abstraction
Decomposition: Large pieces are partitioned into smaller
pieces, each of which is implemented independently.
Composition: Combine smaller units that were separately
defined.
What are these software pieces?
Programming languages support abstractions such as
procedures, modules, user-defined types, and objects.
Abstraction: An abstraction consists of just those essential
properties essential to a purpose.
Information hiding: a technique for supporting various forms
of abstraction. It is usually practiced by controlling the
visibility of names. If a name is hidden within a part of a
program, it cannot be accessed from other parts of the
program.
Access control can make programs easier to read and
maintain.
Information hiding associated with procedures.
ECS140A, Winter’06Abstract Data Types, slide 1 c
Raju Pandey
Data Abstraction
Data abstraction: a programming methodology that supports
modifiability, extensibility, re-usability, encapsulation, etc.
Earlier approaches: Different applications, procedures,
programs access and modify a data structure.
Problems with this approach:
Close coupling between application and data structure
Unnecessar y duplication
Poor support for re-usability
Re-compilation and testing
Conclusions: Poor support for program development
A key insight in program organization is that data and
operations go together.
Restructure the way in which programs are specified:
Data Structure + Procedures that can modify/manipulate
them.
Client/Server view:
Data abstractions: servers
Applications: clients
Data encapsulation: create an encapsulated name space
with mechanisms for accessing entities inside this name
space.
ECS140A, Winter’06Abstract Data Types, slide 2 c
Raju Pandey
Module
A module is a collection of declarations, typically including
both variables and procedures.
Serves as a black box with which the rest of the program
interacts through an interface.
Two parts:
Definition (public): The interface of a module can contain
types, variables, procedures, and so on.
Design questions: What should this part not contain?
Implementation (private): Code for procedures, data
structure definitions, and code for initialization.
Interface between modules via import/export mechanism
Export: determine what should be visible
Import: specify what is being used
Roles of modules:
Suppor t modularization
Improve maintainability
Increase potential for reuse
Separate compilation
Modules in Modula-2: module
Modules in Ada: package
ECS140A, Winter’06Abstract Data Types, slide 3 c
Raju Pandey
Modules
Added after class
What should interface contain?
Type names?
Procedure signatures?
Implementation?
Can I control who can see what
What should implementation contain?
How can other modules use this module?
How to import information?
What if there is name conflict?
Management issues
Do specification and implementation go in the same file?
Different files?
How does compiler find relevant specifications?
ECS140A, Winter’06Abstract Data Types, slide 4 c
Raju Pandey
pf3
pf4

Partial preview of the text

Download Data Abstraction and Modules in Modula-2 and Ada and more Papers Programming Languages in PDF only on Docsity!

Development of complex software systems

• How do we deal with complexity of software development?

Miller: “People can keep track of seven things, be they bits,

words, colors, tones, or tastes. Tentatively, therefore, we are

justified in assuming that our memories are limited by the

number of units or symbols we must master, but not by the

amount of information that these symbols represent.”

• Deal with complexity through decomposition , composition ,

and abstraction

Decomposition: Large pieces are partitioned into smaller

pieces, each of which is implemented independently.

Composition: Combine smaller units that were separately

defined.

• What are these software pieces?

Programming languages support abstractions such as

procedures, modules, user-defined types, and objects.

Abstraction: An abstraction consists of just those essential

properties essential to a purpose.

• Information hiding : a technique for supporting various forms

of abstraction. It is usually practiced by controlling the

visibility of names. If a name is hidden within a part of a

program, it cannot be accessed from other parts of the

program.

Access control can make programs easier to read and

maintain.

Information hiding associated with procedures.

ECS140A, Winter’06Abstract Data Types, slide 1 ©cRaju Pandey

Data Abstraction

• Data abstraction : a programming methodology that supports

modifiability, extensibility, re-usability, encapsulation, etc.

• Earlier approaches: Different applications, procedures,

programs access and modify a data structure.

Problems with this approach:

– Close coupling between application and data structure

– Unnecessary duplication

– Poor support for re-usability

– Re-compilation and testing

Conclusions: Poor support for program development

• A key insight in program organization is that data and

operations go together.

– Restructure the way in which programs are specified:

Data Structure + Procedures that can modify/manipulate

them.

– Client/Server view:

Data abstractions: servers

Applications: clients

• Data encapsulation : create an encapsulated name space

with mechanisms for accessing entities inside this name

space.

ECS140A, Winter’06Abstract Data Types, slide 2 ©cRaju Pandey

Module

• A module is a collection of declarations, typically including

both variables and procedures.

Serves as a black box with which the rest of the program

interacts through an interface.

• Two parts:

– Definition (public) : The interface of a module can contain

types, variables, procedures, and so on.

Design questions: What should this part not contain?

– Implementation (private) : Code for procedures, data

structure definitions, and code for initialization.

• Interface between modules via import/export mechanism

Export: determine what should be visible

Import: specify what is being used

• Roles of modules:

– Support modularization

– Improve maintainability

– Increase potential for reuse

– Separate compilation

• Modules in Modula-2: module

• Modules in Ada: package

Modules

• Added after class

• What should interface contain?

– Type names?

– Procedure signatures?

– Implementation?

– Can I control who can see what

• What should implementation contain?

• How can other modules use this module?

– How to import information?

– What if there is name conflict?

• Management issues

– Do specification and implementation go in the same file?

Different files?

– How does compiler find relevant specifications?

Data abstraction in Modula-

• Modula-2: Descendant of Pascal and Modula; Designed by

N. Wirth; first published 1978, implemented 1979, revised

1984; Directly led to Modula-3 and Oberon

Flavor: Pascal + Modules

• Two separate modules: DEFINITION and IMPLEMENTATION

• Definition module example:

DEFINITION MODULE ComplexNumbers; TYPE COMPLEX: (* opaque type *) PROCEDURE Add (x, y: COMPLEX):COMPLEX; PROCEDURE Subtract (x, y: COMPLEX):COMPLEX; PROCEDURE Multiply (x, y: COMPLEX):COMPLEX; PROCEDURE Divide (x, y: COMPLEX):COMPLEX; PROCEDURE Negate (z: COMPLEX):COMPLEX; PROCEDURE MakeComplex(x, y: REAL): COMPLEX; END ComplexNumbers.

• All functions defined here can be used by other modules

• Note: COMPLEX type’s implementation is hidden. Called

Opaque type.

– Opaque type can only be scalar or pointer types. Why?

The compiler needs to know the size of the type in order

to allocate space for the type.

ECS140A, Winter’06Abstract Data Types, slide 5 ©cRaju Pandey

Data abstraction in Modula-2 - cont’d.

• Implementation module :

IMPLEMENTATION MODULE ComplexNumbers; FROM Storage IMPORT ALLOCATE; TYPE COMPLEX = POINTER TO ComplexRecord; ComplexRecord = RECORD re, im: REAL; END; PROCEDURE Add(x, y: COMPLEX):COMPLEX; VAR t: COMPLEX; BEGIN NEW(t); tˆ.re := xˆ.re + yˆ.re; tˆ.im := xˆ.im + yˆ.im; END Add ...

Note: COMPLEX is defined as a pointer.

• Usage of COMPLEX:

MODULE ComplexUser; FROM ComplexNumbers IMPORT COMPLEX, Add, MakeComplex; VAR x, y, z: COMPLEX; BEGIN x := MakeComplex(1.0, 2.0); y := MakeComplex(-1.0, 1.0); ... z := Add(x, y); END

• Context clause: FROM clause enumerates the functions

associated with the module. Without FROM clause, need to

reference the module name as well.

x := ComplexNumbers.MakeComplex(1,0,2.0);

ECS140A, Winter’06Abstract Data Types, slide 6 ©cRaju Pandey

Data abstraction in Ada

• Ada: (early 1970’s) DoD undertook to develop a standard

programming language. A set of detailed specifications were

produced: Strawman, Woodenman, Tinman, Ironman,

Steelman. Final specification, Ada, was chosen from among

17 entries.

• Supports modules through package : package specification

and package body.

• Example package specification:

package ComplexNumbers is type COMPLEX is private; function Add(x, y: in COMPLEX) return COMPLEX; function Subtract(x, y: in COMPLEX) return COMPLEX; function Multiply(x, y: in COMPLEX) return COMPLEX; function Divide(x, y: in COMPLEX) return COMPLEX; function Negate(z: in COMPLEX) return COMPLEX; function MakeComplex(x, y: in FLOAT) return COMPLEX; private type COMPLEX is record re, im: FLOAT; end; end ComplexNumbers;

• private ⇒ implementation is inaccessible.

Disadvantage: if implementation changes ⇒ change

specification.

• Can remove from the specification section by making it a

pointer (just as in Modula-2).

• Only default operations on private type are equality and

assignment.

Data abstraction in Ada

• Package body:

package body ComplexNumbers is

function Add(x, y: in COMPLEX) return COMPLEX is t: COMPLEX; begin t := new ComplexRecord; t.re := x.re + y.re; t.im := x.im + y.im; end Add; ... end ComplexNumbers

• Usage of ComplexNumbers:

with ComplexNumbers; use ComplexNumbers; procedure ComplexUser is z, w: COMPLEX; ... begin z := MakeComplex(1.0, 1.0); ... w := Add(z, z); end ComplexUser;

• with statement is similar to FROM of MODULA-2:

dereference the package name.

Interfaces in Java

• Description of behavior; define a type.

• Method specifications in an abstract definition

– Implicitly abstract

– Always public

– No implementation

– No static methods

• Fields:

– No instance variables

– Only static and final (akin to declaring constants)

• Implementing interfaces: Classes implement specific sets of

interfaces. Must implement all methods

Interface StackInterface { int MAXSTACKSIZE = 100; public char pop; public void push(char val); } class StackArray Implements StackInterface { private char elements[]; public char pop() { return elements[top]; } public void push(char elem) { ...} ... } class StackLinkedList Implements StackInterface { private CharLinkedList elementsList; public char pop() { ... } public void push(char elem) { ...} ... }

ECS140A, Winter’06Abstract Data Types, slide 13 ©cRaju Pandey

Objects in C++

• C++ developed at AT&T Bell Lab during 1979-1991 by Bjarne

Stroustrup. An evolutionary approach to programming

language design: (i) Keep C’s efficiency (ii) Add data

abstraction, inheritance, genericity, operator overloading, (iii)

Do not need to learn a new language or construct new tools

from scratch.

• Classes: Generalization of structures of C to Group both data

and operations

A class also defines a type.

class Stack { public: char pop(); void push(char); private: int top; char elements[10]; };

• Access control : through public, private, and

protected sections: determine what clients of a class

can access.

• Friends : Mechanism by which a class can grant client

classes/functions accesss to nonpublic members:

class Stack { friend ostream& operator<<(ostream&, const Stack&); friend istream& operator>>(istream&, Stack&); ... };

• Separate compilation: header and .cc files

ECS140A, Winter’06Abstract Data Types, slide 14 ©cRaju Pandey

Special member functions of classes

• Constructor:

– A function with same name as class + with no return type;

Called when an object is created.

Stack() { top = 0;} .. Stack X[100];

– Constructor for Stack called 100 times in order of

increasing address.

– Can define any number of constructors with different

typed parameters

– Default constructor

• Destructor:

– Destructor called when object is deleted or goes out of

scope.

˜Stack();

– Can invoke destructor explicitly:

Stack *x; ... x->˜Stack(); // does not deallocate space

• Copy constructor: called to copy an object of a class.:

Stack(const Stack &);

• Type conversions:

operator Stack::int() { ... } ... Stack x; int i = int(x);

• Compiler generates default constructor and copy constructor.

Summary

• How should accesses to a data structure be managed?

By bringing code and data together.

• How to support abstraction, modularity and re-usability?

– Data abstraction

– Separation of definition and implementation

– Control over accesses to implementation

• How do programming languages support this?

– Modules: Ada, MODULA-

– Types: Euclid

– Classes: SmallTalk, C++, Eiffel, Java

• C++ classes: Definition + Implementation + special functions:

– Constructors, Destructors..

– Copy constructors, etc..

– Special functions can be supplied by the user or the

compiler.