modular it y

8
Module: a unit of organization of a software system. groups together some functions, data, types, etc. Example: various string functions in C's standard string library. • hides certain details from users (information hiding). e.g. local variables in a C function are inaccessible to callers of the function. Modularity

Upload: taharazvi

Post on 23-Dec-2015

213 views

Category:

Documents


1 download

DESCRIPTION

Algorithms

TRANSCRIPT

Page 1: Modular It y

Module: a unit of organization of a software system.

• groups together some functions, data, types, etc.

Example: various string functions in C's standard string

library.

• hides certain details from users (information hiding). e.g. local variables in a C function are inaccessible to callers of the function.

Modularity

Page 2: Modular It y

Users and implementers of a module have different views of it.

Interface: user’s view of a module.

• describes only what a user needs to know to use the module• makes it easier to understand and use• describes what services the module provides, but not how

it’s able to provide them

Interface vs. Implementation

Page 3: Modular It y

• Users can develop their code before the module is implemented.

• User code and module code can be compiled separately.

• Module’s implementation can change (e.g. bug fixes, better data structures and algorithms) without changing or recompiling user code.

Technical benefits of an interface

Page 4: Modular It y

Interface: header (.h) file containing declarations of functions, constants, variables, types and macros for public (global) use.

Also includes comments for users.

Implementation: source (.c) file containing definitions of the functions, along with local constants, variables, types and macros for private use.

Also includes comments for implementers.

Modules in C

Page 5: Modular It y

Users #include the header files of modules they use. This is enough to compile the user’s code.

Modules also include their header file, so the compiler can check for consistency before the linking stage.

The next slide shows an interface to the linked list services in the sample code linklist2.c.

Modules in C, cont’d

Page 6: Modular It y

A header file for linklist2.c

/* Sorted linked lists of chars */ /* Linked list nodes are of type node_t */typedef struct element { char contents; struct element *next;} node_t; /* Inserts new node with contents `key', keeping sorted. `ptr_to_head’: address of pointer to head of list */int insertNode(node_t **ptr_to_head, char key);/* ... */int deleteNode(node_t **ptr_to_head, char);/* ... */void traverse(node_t *head);/* ... */void deleteAllNodes(node_t **);

Page 7: Modular It y

Abstract Data Type (ADT): A set of values and the core operations on those values, independent of a particular (computer) representation.

Examples include stacks and lists.

A description of the Stack ADT:

A stack holds values which can be inserted and removed (called pushing and popping). Values are kept in the order in which they were inserted, and removed in the reverse order (first in last out).

Data Abstraction

Page 8: Modular It y

There is only one stack ADT, but there are many possible implementations of it. An implementation would use some data structure, and implement the operations with functions manipulating the data structure.

In C, one might implement the stack ADT with a linked list or an array. The operations on the stack ADT would be functions declared in the interface. The chosen data structure, and how the functions manipulate the data structure would be implementation details hidden from the user.

Data Abstraction and Modules