CSC 509 Lecture Notes Week 7
CSC 509 Lecture Notes Week 7
Guidelines for Module Coupling and Cohesion
-
Coupling and cohesion between modules (i.e., classes).
-
"Coupling" and "cohesion" are terms used to denote module interconnectedness.
-
Coupling refers to how much inter-module connection exists in a design;
specific measures of coupling are:
-
the number of classes that a given class inherits from;
-
the number of classes that a given class references as data members;
-
the number of methods that a given method calls;
-
the number of parameters that a given method has.
-
Cohesion refers to how much intra-module cohesiveness a design has; measures of
cohesion are:
-
the degree to which all methods in a class perform a logically-related aspect
of system functionality
-
the degree to which a method performs a single specific function
-
In general, coupling should be minimized and cohesion should be maximized.
-
Techniques to reduce coupling
-
Limit the number of classes that communicate with (i.e, call functions of)
other classes.
-
E.g. in an MVP design, a Process-class method invoked by a View class method
may forward a Model-class exception rather than catching the exception and
calling a View-class method.
-
E.g.,
-
n the Rolodex system, the Process-class method
RolodexCommandProcessor.processCommand is called from a View-class method in
RolodexCommandEditor.
-
The processCommand method throws an InputErrors exception back to the view,
rather than catching the exception and calling a View class.
-
This limits coupling by not having a View class reference in the
RolodexCommandProcessor process class.
-
Limit the number of calls between classes that do need to communicate with each
other.
-
E.g., in the Rolodex system, the dialog button functions, such as OKAddButton,
can communicate directly with the model, without having to go through a parent
view class.
-
E.g., OKAddButton.actionPerformed can call Rolodex.add
directly
-
More highly-coupled alternative would be some chain-of-command call such as
AddCardDialog.getRolodex().add.
-
Limit the number of public functions in each class.
-
In general, make a function public only if there is a demand for it from some
other class.
-
Do not add public get functions "in case" they may be needed; the same
goes for public set functions.
-
E.g., since AddCardDialog.getRolodex is never used, it should not be provided.
-
Limit the types of function parameters and return values to the smallest type
necessary.
-
Consider the Rolodex.find operation.
-
It returns zero or more cards, which could be returned as Rolodex object, or a
simpler CardList object.
-
The CardList object is preferable, since callers of Rolodex.find do not need
all of the information in a full Rolodex, but only a simple list.
-
Design to limit the amount of change needed if data representations change.
-
When using external process classes (e.g., C++ from Java), design
wrapper classes that define a generic process class that hides details
of external class.
-
With a properly designed wrapper class, little or no changes to a Model class
are necessary when changing underlying data representations in Process classes.
-
Use exception handling wisely.
-
Consider again the use of exception handling to communicate between models and
views.
-
The InputErrors exception classes provide a generic, string-based
representation that can be used uniformly for error communication.
-
Techniques to increase cohesion
-
Generally, coupling measures are easier to pinpoint exactly.
-
Limiting the coupling between classes in turn increases cohesiveness, since
decoupling eliminates unnecessary items from classes.
-
To address cohesion directly means to design each class to represent a single,
clearly identifiable piece of data and/or encapsulate a single clearly
identifiable area of functionality.
-
To a large extent, the MVP design technique promotes cohesion, since it has
well-defined functionality for each category of design class.
-
Limiting the size of each individual scoping unit (class or function), per our
design and implementation conventions, is also a way to foster cohesion.
-
I.e., it's harder to do too much in a class or function if you can't do that
much there in the first place.
-
Ease of debugging -- a major rationale for limiting coupling.
-
Consider the debugging question: "If something goes wrong in module X, where do
I look for the problem?"
-
First, look in module X for the problem.
-
If it's caused immediately there, then look at all other modules that connect
to X.
-
Repeat this process until the source of the problem is located.
-
Clearly, this problem isolation process will be made more orderly, and
potentially shorter if the number of directly connected modules is limited.
index
|
lectures
|
handouts
|
examples
|
doc