Granularity

On a scale ranging from fine to coarse-grained, modules are available to the programmer as follows:

  1. method (function or procedure)
  2. class
  3. package
  4. component
  5. program

Each of these types of module have elements in common:

  1. each has a name
  2. they reside in a library until they are needed

We will now look at each feature in turn.

A method carries out a single task. It has a name, can take arguments (parameters) and can return a value (as the value of a function) or via the arguments. A method usually has no state - it is incapable of recalling any previous usage. The programmer using a procedure or function needs to know what the procedure or function does, but needs no knowledge of how it works.

A class is a general-purpose template which allows any number of instances to be created in its likeness. An instance is often termed an object. An object provides a number of services as methods. An object usually has state - the values of variables held within the object. Objects provide encapsulation and information hiding. A user program creates an instance of a class at run time and thereafter makes calls on its methods. Again, a programmer using a procedure or function needs to know what the procedure or function does, but needs no knowledge of how it works.

Classes also support re-use through inheritance. A new class can be declared which uses the facilities of the base class, but adds new methods. It may also redefine (the correct term is over-ride) methods in the base class. A distinctive feature of inheritance re-use is that the programmer needs access to the workings of the base class - and therefore the source code. A new inheriting class is created at compile time.

A package is a collection of related classes. Examples are a package of classes that support GUI's, a package of file handling classes, a package of classes that support internet intercommunication between programs. The purpose of a package is to provide a higher level of modularity than a class - typically a package contains 10's of classes. But in addition, packages provide control over access to methods within classes. For example, any code can access any protected method of classes within the same package, but not elsewhere.

A component is a software module that supports a number of end-user features, but can be used by an application. Like an object, a component provides a set of services via calls to methods. However, the user of a component does not need to instantiate a component (as is necessary using a class.) The internal structure of a component is invisible to the user programmer. (A component may consist of a number of classes and objects.) Examples of component types are Java's Beans and Microsoft's COM, DCOM and ActiveX.

A program is a software module that runs autonomously. It need not be invoked by another software module. Examples are a word processor, a compiler, a web browser, a spreadsheet. A program is not completely independently of other modules, since a program typically uses other components as it executes. Although we may think of a user ( a human) invoking a program via a GUI or a command line interface, in reality it is of course the operating system that invokes a program. Thus any type of software module is invariably invoked by some other software module.

Programs are sometimes invoked from a script written in a scripting language. Examples of such languages are the Unix shell, Perl, Visual Basic.

A summary comparison the characteristics of the different modularity mechanisms is given below:

mechanism

example

typical size

example programming languages

method

calculate square root of a number

10's of lines of code

(all)

class

A Button class to support button GUI objects

several methods

C++, Java, VB

package

GUI classes for buttons, text fields, menus, etc

several classes

Java

component

winsock - Microsoft's collection of methods to carry out communication between programs across the Internet

1000's of lines of code

Java Beans, Microsoft activeX

program

web browser

millions of lines of code

(all)

 

Exercises

Explore information on C++, Ada, Java, Visual Basic and review each of their facilities for modularity. Compare and contrast the facilities for modularity provided by C++, Ada, Java, Visual Basic.