Java Reference
In-Depth Information
another program, we have to take with it all the classes on which it depends. Thus, we
want to remove unnecessary coupling between classes.
It is a good practice to minimize the coupling (i.e., dependency) between classes.
S
ELF
C
HECK
3.
Why is the
CashRegister
class from
Chapter 4
not cohesive?
4.
Why does the
Coin
class not depend on the
CashRegister
class?
5.
Why should coupling be minimized between classes?
Q
UALITY
T
IP
8.1: Consistency
In this section you learned of two criteria to analyze the quality of the public
interface of a class. You should maximize cohesion and remove unnecessary
coupling. There is another criterion that we would like you to pay attention toȌ
consistency. When you have a set of methods, follow a consistent scheme for their
names and parameters. This is simply a sign of good craftsmanship.
Sadly, you can find any number of inconsistencies in the standard library. Here is
an example. To show an input dialog box, you call
JOptionPane.showInputDialog(promptString)
To show a message dialog box, you call
JOptionPane.showMessageDialog(
null
, messageString)
What's the
null
parameter? It turns out that the
showMessageDialog
method
needs a parameter to specify the parent window, or
null
if no parent window is
required. But the
showInputDialog
method requires no parent window. Why
the inconsistency? There is no reason. It would have been an easy matter to supply
a
showMessageDialog
method that exactly mirrors the
showInputDialog
method.
Inconsistencies such as these are not a fatal flaw, but they are an annoyance,
particularly because they can be so easily avoided.
340