Anda di halaman 1dari 4

Development of complex software systems Data Abstraction

• How do we deal with complexity of software development? • Data abstraction: a programming methodology that supports
Miller: “People can keep track of seven things, be they bits, modifiability, extensibility, re-usability, encapsulation, etc.
words, colors, tones, or tastes. Tentatively, therefore, we are • Earlier approaches: Different applications, procedures,
justified in assuming that our memories are limited by the programs access and modify a data structure.
number of units or symbols we must master, but not by the
Problems with this approach:
amount of information that these symbols represent.”
– Close coupling between application and data structure
• Deal with complexity through decomposition, composition,
– Unnecessary duplication
and abstraction
– Poor support for re-usability
Decomposition: Large pieces are partitioned into smaller
pieces, each of which is implemented independently. – Re-compilation and testing
Composition: Combine smaller units that were separately Conclusions: Poor support for program development
• A key insight in program organization is that data and
• What are these software pieces? operations go together.
Programming languages support abstractions such as – Restructure the way in which programs are specified:
procedures, modules, user-defined types, and objects. Data Structure + Procedures that can modify/manipulate
Abstraction: An abstraction consists of just those essential them.
properties essential to a purpose. – Client/Server view:
• Information hiding: a technique for supporting various forms Data abstractions: servers
of abstraction. It is usually practiced by controlling the Applications: clients
visibility of names. If a name is hidden within a part of a • Data encapsulation: create an encapsulated name space
program, it cannot be accessed from other parts of the with mechanisms for accessing entities inside this name
program. space.
Access control can make programs easier to read and
Information hiding associated with procedures.

ECS140A, Winter’06Abstract Data Types, slide 1

c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 2
c Raju Pandey

Module Modules
• A module is a collection of declarations, typically including • Added after class
both variables and procedures.
• What should interface contain?
Serves as a black box with which the rest of the program
– Type names?
interacts through an interface.
– Procedure signatures?
• Two parts:
– Implementation?
– Definition (public): The interface of a module can contain
– Can I control who can see what
types, variables, procedures, and so on.
Design questions: What should this part not contain? • What should implementation contain?
– Implementation (private): Code for procedures, data • How can other modules use this module?
structure definitions, and code for initialization.
– How to import information?
• Interface between modules via import/export mechanism – What if there is name conflict?
Export: determine what should be visible
• Management issues
Import: specify what is being used
– Do specification and implementation go in the same file?
• Roles of modules: Different files?
– Support modularization – How does compiler find relevant specifications?
– 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 ECS140A, Winter’06Abstract Data Types, slide 4
c Raju Pandey
Data abstraction in Modula-2 Data abstraction in Modula-2 - cont’d.
• Modula-2: Descendant of Pascal and Modula; Designed by • Implementation module:
N. Wirth; first published 1978, implemented 1979, revised FROM Storage IMPORT ALLOCATE;
1984; Directly led to Modula-3 and Oberon TYPE COMPLEX = POINTER TO ComplexRecord;
ComplexRecord = RECORD
Flavor: Pascal + Modules re, im: REAL;
• Definition module example: BEGIN
DEFINITION MODULE ComplexNumbers; tˆ.re := xˆ.re + yˆ.re;
TYPE COMPLEX: (* opaque type *) tˆ.im := xˆ.im + yˆ.im;
PROCEDURE Divide (x, y: COMPLEX):COMPLEX; Note: COMPLEX is defined as a pointer.
PROCEDURE MakeComplex(x, y: REAL): COMPLEX; • Usage of COMPLEX:
END ComplexNumbers.
MODULE ComplexUser;
• All functions defined here can be used by other modules FROM ComplexNumbers IMPORT
COMPLEX, Add, MakeComplex;
VAR x, y, z: COMPLEX;
• Note: COMPLEX type’s implementation is hidden. Called BEGIN
Opaque type. x := MakeComplex(1.0, 2.0);
y := MakeComplex(-1.0, 1.0);
– Opaque type can only be scalar or pointer types. Why? ...
z := Add(x, y);
The compiler needs to know the size of the type in order END
to allocate space for the type.
• 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 5

c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 6
c Raju Pandey

Data abstraction in Ada Data abstraction in Ada

• Ada: (early 1970’s) DoD undertook to develop a standard • Package body:
programming language. A set of detailed specifications were package body ComplexNumbers is
produced: Strawman, Woodenman, Tinman, Ironman, function Add(x, y: in COMPLEX) return COMPLEX is
Steelman. Final specification, Ada, was chosen from among t: COMPLEX;
17 entries. t := new ComplexRecord; := +;
• Supports modules through package: package specification := +;
and package body. end Add;
• Example package specification: end ComplexNumbers
package ComplexNumbers is
type COMPLEX is private;
• Usage of ComplexNumbers:
function Add(x, y: in COMPLEX) return COMPLEX; with ComplexNumbers;
function Subtract(x, y: in COMPLEX) return COMPLEX; use ComplexNumbers;
function Multiply(x, y: in COMPLEX) return COMPLEX; procedure ComplexUser is
function Divide(x, y: in COMPLEX) return COMPLEX; z, w: COMPLEX;
function Negate(z: in COMPLEX) return COMPLEX; ...
function MakeComplex(x, y: in FLOAT) return COMPLEX; begin
private z := MakeComplex(1.0, 1.0);
type COMPLEX is ...
record w := Add(z, z);
re, im: FLOAT; end ComplexUser;
end ComplexNumbers; • with statement is similar to FROM of MODULA-2:
• private ⇒ implementation is inaccessible. dereference the package name.

Disadvantage: if implementation changes ⇒ change

• 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

ECS140A, Winter’06Abstract Data Types, slide 7

c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 8
c Raju Pandey
Separate Compilation Packages and Types
• Divide large programs into modules each of which can be • Modules are not types
compiled separately. – Needs ad hoc mechanism for exporting local types +
• DEFINITION module compiled to a symbol table. needs special procedures for initializing and deleting
• A module A is dependent on another module B if it uses
– Can locate many types together
definitions of B
• Modules are static entities:
• Dependencies determine Order in which modules must be DEFINITION MODULE IntStack;
compiled: PROCEDURE Push(val: INTEGER);
– Module A importing from module B must be compiled END IntStack;
after B has been compiled.
• Dependencies expressed as a graph, called Compilation CONST SIZE = 100;
VAR data: ARRAY[1..Size] of INTEGER;
dependency graph. For a correct compilation order, there top: [0..SIZE];
should be no cycles in the graph. PROCEDURE Push (val: INTEGER);
• Management of module libraries by the compiler: changes in INC(top);
data[top] := val;
module ⇒ recompile all modules that depend on this END Push;
module. ...
END IntStack
• Ada and MODULA-2 both support separate compilation MODULE foo1;
FROM IntStack IMPORT Push, Pop, Top;
• Link time dependency graph: Main body of blocks may also VAR i: INTEGER;
need to be executed in specific order ⇒ code must be linked BEGIN
push(10); push(100);
according to their dependencies ...
END foo1
• In Ada, individual functions can be compiled separately as
well. • Only one stack module exists. For more stacks, need to
package body A is create different stack module.
function f(x:FLOAT) return FLOAT is separate Initialization code for module executed only once
end A; • Types allow multiple instances to be created.
ECS140A, Winter’06Abstract Data Types, slide 9
c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 10
c Raju Pandey

Classes and Objects Classes and Objects in Java

• Two views: • Classes: Generalization of structures of C to group both data
External: Objects emulate real world entities and operations
A class in Java also defines a type.
Internal: capture certain states and respond to events.
class Stack {
private int top;
• First incorporated in Simula (1963). private char elements[];
public char pop() { ... }
• Object = state + methods public void push(char val) { ...}
public Stack();
• Objects interact by sending messages to each other. Each }
object is like a separate computer with its own code and its • Definition of a class may include
own private memory to hold values. A program consists of a
1. Fields (Static and Instance)
set of objects that execute by acting and reacting to each
other. 2. Methods (Static and Instance)

• Terminology: • Access control: through public, private, and

protected declarations; determine what clients of a
– Object: A collection of data and operations
class can access.
– Class: pattern for objects
• Special member functions:
– Instances of a class: an object 1. Constructor: A function with same name as class + with
– Method: A procedure body implementing an operation no return type; Called when an object is created.
Stack() { top = 0;}
associated with an object ...
Stack x = new Stack();
– Message: A procedure call; request to execute a method
Constructor for Stack called;
• How do classes support data abstraction? Can define any number of constructors with different
• How do they support extensibility? typed parameters
Default constructor.
2. Finalizers: Finalizer called when object is garbage
collected. Used to relinquish resources (for instance, files)
that object may have collected.
protected void finalizer() {... }

• How are classes used?

ECS140A, Winter’06Abstract Data Types, slide 11
c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 12
c Raju Pandey
Interfaces in Java Objects in C++
• Description of behavior; define a type. • C++ developed at AT&T Bell Lab during 1979-1991 by Bjarne
Stroustrup. An evolutionary approach to programming
• Method specifications in an abstract definition
language design: (i) Keep C’s efficiency (ii) Add data
– Implicitly abstract abstraction, inheritance, genericity, operator overloading, (iii)
– Always public Do not need to learn a new language or construct new tools
– No implementation from scratch.

– No static methods • Classes: Generalization of structures of C to Group both data

and operations
• Fields: A class also defines a type.
– No instance variables class Stack {
– Only static and final (akin to declaring constants) char pop();
void push(char);
• Implementing interfaces: Classes implement specific sets of private:
interfaces. Must implement all methods int top;
char elements[10];
Interface StackInterface { };
public char pop; • Access control: through public, private, and
public void push(char val);
} protected sections: determine what clients of a class
class StackArray Implements StackInterface { can access.
private char elements[];
public char pop() { return elements[top]; } • Friends: Mechanism by which a class can grant client
public void push(char elem) { ...} classes/functions accesss to nonpublic members:
} class Stack {
class StackLinkedList Implements StackInterface { friend ostream& operator<<(ostream&, const Stack&);
private CharLinkedList elementsList; friend istream& operator>>(istream&, Stack&);
public char pop() { ... } ...
public void push(char elem) { ...} };
} • Separate compilation: header and .cc files

ECS140A, Winter’06Abstract Data Types, slide 13

c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 14
c Raju Pandey

Special member functions of classes Summary

• Constructor: • How should accesses to a data structure be managed?
– A function with same name as class + with no return type; By bringing code and data together.
Called when an object is created.
Stack() { top = 0;} • How to support abstraction, modularity and re-usability?
Stack X[100]; – Data abstraction
– Constructor for Stack called 100 times in order of – Separation of definition and implementation
increasing address. – Control over accesses to implementation
– Can define any number of constructors with different
typed parameters
• How do programming languages support this?
– Modules: Ada, MODULA-2
– Default constructor
– Types: Euclid
• Destructor:
– Destructor called when object is deleted or goes out of – Classes: SmallTalk, C++, Eiffel, Java
˜Stack(); • C++ classes: Definition + Implementation + special functions:
– Can invoke destructor explicitly: – Constructors, Destructors..
Stack *x;
... – Copy constructors, etc..
x->˜Stack(); // does not deallocate space – Special functions can be supplied by the user or the
• Copy constructor: called to copy an object of a class.: compiler.
Stack(const Stack &);
• Type conversions:
operator Stack::int() {
Stack x;
int i = int(x);

• Compiler generates default constructor and copy constructor.

ECS140A, Winter’06Abstract Data Types, slide 15

c Raju Pandey ECS140A, Winter’06Abstract Data Types, slide 16
c Raju Pandey