Modularity: By: Andre Mayne All Rights Reserved ©
Modularity: By: Andre Mayne All Rights Reserved ©
What Is a Module?
Insoftware, a module is a part of
aprogram. Programs are
composed of one or more
independently developed modules
that are not combined until the
program islinked. A single module
can contain one or
severalroutines.
Notable features
Int main ()
{
char *strings[MAX_STRINGS];
Int nstrings;
ReadStrings(strings, &nstrings,
MAX_STRINGS, stdin);
SortStrings(strings,nstrings);
WriteStrings(strings,_nstrings, stdout);
return_0;
}
Easier to understand
Easier to test and debug
Easier to reuse code
Easier to make changes
Easier to understand
Easier to test and debug
Easier to reuse code
Easier to make changes
Easier to understand
Easier to test and debug
Easier to reuse code
Easier to make changes
Easier to understand
Easier to test and debug
Easier to reuse code
Easier to make changes
Separate Compilation
Move string array into separate file
Declare interface instringarray.h
Provide implementation instringarray.c
Allows re-use by other programs
stringarray.h
Extern_void_ReadStrings(char **strings,int*nstrings,
Int maxstrings, FILE *fp);
Extern void WriteStrings(char **strings,int nstrings, FILE *fp);
Extern void SortStrings(char **strings,int nstrings);
Externint CompareStrings(char *string1, char *string2);
Abstract_Data_Types
Module supporting operations on single data structure
Interface declares operations, not data structure
Implementation is hidden from client (encapsulation)
Use features of programming language to ensure encapsulation
Common_practice
Allocation and deallocation of data structure handled by
module
Names of functions and variables begin with <modulename>
Provide as much generality/flexibility in interface as possible
Use void pointers to allow polymorphism
Example ADT-Interface
array.h
#ifndefARRAY_H
#define_ARRAY_H
typedef_struct_Array_*Array_T;
extern_Array_T_Array_new(void);
extern_void_Array_free(Array_T_array);
extern_void_Array_insert(Array_T_array,_void_*datap);
extern_void_Array_remove(Array_T_array,_void_*datap);
extern_intArray_getLength(Array_T_array);
extern_void_*Array_getKth(Array_T_array,_intk);
#endif
Summary