Program Design (プログラム設計)

7. Modular design
(モジュール 設計)
Modular design is an important technique for
designing good quality program systems.
In this part, you will learn:(内容)
The definition of module(モジュールの定義)
Module strength or cohesion(モジュールの強度 又は結
合力 )
Module coupling(モジュール連結)
Inter-module communication(モジュールの間の交信)
Information hiding(情報隠蔽)
7.1 The definition of module
(モジュールの定義)
There is no unique definition of module.
Module is just a notion that represents a program
unit with the following features:
A module provides functional services. For example,
given an input, a module performs a function or behavior.
A module can be replaced by another module
implementing the same functional specification, with
little or no impact on the entire program.
The idea of modular design is similar to that of designing
other products, such as cars, houses, computers, and so on.
Specifically, a module can be interpreted as one of
the following program units:
A procedure in Pascal.
A function in C.
These are primitive
understanding of
An operation in general.
module.
A process in DFDs.
A method in Java.
These are
A unit that groups operations.
advanced
A class (e.g., a Java class).
understanding of
module.
Examples
(1) Primitive notion of module: a Java method:
int Swap(int x, int y)
{
int a = 0; //local variable declaration
a = x;
x = y;
y = a;
}
//statement-1
//statement-2
//statement-3
(2) Advanced notion of module:
module A;
variable declarations;
operation-1;
operation-2;
operation-3;
…
operation-n
end-module
A program unit that
groups operations
together.
A Java class:
class Calculator {
int reg;
public Calculator() {
initialize reg;
} //constructor of the class
public int Add(int i) {
reg = reg + i;
}
public int Subtract(int i) {
reg = reg – i;
}
public int Multiply(int i) {
reg = reg * i;
}
}
7.2 Module strength or cohesion
(モジュールの強度 又は結合力 )
We use the primitive notion of module in the
discussions from now on in this part, since we will
discuss the related issues based on the advanced
notion of module later in this course.
Description: module strength or cohesion is a
measure of the extent to which the statements or
functions in a module belong together. (モジュー
ルの強度又は結合力は、文又は操作が同じモ
ジュールに所属する程度を測る尺度である)
In general, if statements are related closely in
performing a single task or common tasks, they
should belong to the same module. (同じタスクを
完成するために必要な文又は操作が同じモ
ジュールに所属する訳)
However, since there is no unique interpretation of
the meaning of a single task and common tasks,
there is no precise standard to define what
statements should be used in the same module.
There are some guidelines for designing modules.
Guidelines for designing modules
モジュール設計の指針)
The statements are all necessary to perform one
particular task within the overall program structure. For
example, Add, Subtract, and Mutiply in class Calculator
can be individual modules.
The statements encompass a variety of small unrelated
tasks, but since they must be performed at the same time,
for convenience they are grouped in a single module.
For example, the initialization of global variables.
The statements perform two or more related tasks that
are always performed together and therefore have been
placed in the same module. For example, writing data to
files, at the end of an execution of a program.
7.3 Module coupling
(モジュール連結)
Description: module coupling is a measure of the
interdependence between modules, within a program
system.
In general, the stronger the module coupling is, the more
possible one of the modules is affected by changing the
other module. For example, the small families and big
family systems can help explain this issue.
In general, module coupling through global variables are
stronger than those that couple through arguments
(parameters).
Example
class Calculator {
int reg;
void Calculator() {
initialize reg;
} //constructor of the class
int Add(int i) {
reg = reg + i;
Use the
}
same
int Subtract(int i) {
global
variable
reg = reg – i;
reg.
}
int Multiply(int i) {
reg = reg * i;
}
}
Use no
common
global
variable
class Calculator {
int reg;
void Calculator() {
initialize reg;
} //constructor of the class
int Add(int reg, int i) {
reg = reg + i;
}
int Subtract(int reg, int i) {
reg = reg – i;
}
int Multiply(int reg, int i) {
reg = reg * i;
}
}
7.4 Inter-module communication
(モジュールの間の交信)
A program is usually composed of related modules.
A module is related to other modules in the manner
that either it is invoked (or called) by other modules
or it invokes (or calls) other modules.
When a module invokes another module, the communication
by passing data items may be necessary. The problem is
how modules can communicate with each other, and what
are advantages and disadvantages of each communication
method.
Communication methods
(交信手段)
Communication by arguments
Communication by global (or external) variables.
Let’s take the following program as an example to
explain the meaning of these two methods.
Problem: input 10 non-zero integers and output the
two numbers: (1) the number of integers that is
greater than the average of the input integers; (2)
the number of integers that is smaller than the
average of the input integers. For example,
input: 5, 78, 56, 89, 12, 24, 75, 98, 34, 68 (average = 539 /
10 = 53.9);
output: count1 = 6 (the greater numbers: 78, 56, 89, 75, 98,
68)
count2 = 4 (the smaller numbers: 5, 12, 24, 34)
Two Java-based pseudocode
Program 1:
import java.io.*;
public class CountNumbers { //there is no global variables here
public static void main(String args[]) {
int numbers[] = new int[10];
int count1 = 0, count2 = 0;
double average = 0.0;
for (int i = 0; i < numbers.length; i++)
INPUT numbers[i];
average = CalculateAverage(numbers);
count1 = CountGreater(average, numbers);
count2 = CountSmaller(average, numbers);
OUTPUT count1 and count2;
}
double CalculateAverage(int a[]) //obtain average
{
int total = 0;
double ave = 0.0;
for (int j = 0; j < a.length; j++)
total = total + a[j];
ave = total / 10;
return ave;
}
int CountGreater(double ave, int a[]) {
for (int k = 0; k < a.length; k++)
if (a[k] > ave)
count1 = count1 + 1;
return count1;
}
int CountSmaller(double ave, int a[]) {
for (int k = 0; k < a.length; k++)
if (a[k] < ave)
count2 = count2 + 1;
return count2;
}
}
Program 2:
import java.io.*;
public class CountNumbers { //there are four global variables here
static int numbers[] = new int[10];
static double average = 0.0;
static int count1 = 0, count2 = 0;
public static void main(String args[]) {
for (int i = 0; i < numbers.length; i++)
for (int l = 0; l < numbers.length; l++)
numbers[l] = l +1;
CalculateAverage();
CountGreaterSmaller(); //compute count1 and count2
System.out.println("Count1:" + count1);
System.out.println("Count2:" + count2);
}
static void CalculateAverage() { // Obtain average
int total = 0;
for (int j = 0; j < numbers.length; j++)
total = total + numbers[j];
average = total / 10;
}
static void CountGreaterSmaller() {
for (int k = 0; k < numbers.length; k++)
if (numbers[k] > average)
count1 = count1 + 1;
else
count2 = count2 + 1;
}
}
Advantages and disadvantages
(利点と弱点)
Communication by global variables strengthens module
coupling, therefore decreases the independence of
modules. This is not a good situation from the modularity
point of view.
However, the efficiency of communication may be high,
because there is no need to copy any values when passing
arguments to parameters of modules.
Communication by arguments reduces module
coupling, therefore improve the independence of
modules. This is usually regarded as a good
quality of program, because the change of one
module will produce less or no impact on other
modules. But the efficiency of communication
may be low, because there is a need to copy
values when passing arguments to parameters of
modules.
7.5 Information hiding
(情報隠蔽)
Information hiding is a quality that all of the
implementation details, including local variables
and algorithms, of a module are hidden from other
modules or parts of the program.
In other words, a module can only be used by other modules
through invocation or calling of that module to perform its
task, but cannot access the local variables and code of the
algorithm of the module.
Example
import java.io.*;
public class CountNumbers { //there are four global variables here
static int numbers[] = new int[10];
static double average = 0.0;
static int count1 = 0, count2 = 0;
public static void main(String args[]) {
for (int i = 0; i < numbers.length; i++)
for (int l = 0; l < numbers.length; l++)
numbers[l] = l +1;
CalculateAverage();
CountGreaterSmaller(); //compute count1 and count2
System.out.println("Count1:" + count1);
System.out.println("Count2:" + count2);
}