(^290) | Object-Oriented Software Design and Implementation
The responsibilities do not include any transformers, so an instance of this class is an im-
mutableobject; once created, it doesn’t change. How do we know that the instance doesn’t
change? No transformer methods are defined for the class.
Now let’s look at how we turn this CRC card into a design for a class. The data abstrac-
tion of an object can be provided to the user in two ways: in the fields that it makes publicly
accessible and in the parameters of its methods. We briefly consider publicfield values, and
then look at methods and parameters. The data fields are areaCodeand digits.
int areaCode;
int digits;
Because the observer responsibilities return the values of these fields, we might consider
simply declaring the corresponding fields to bepublicand letting the user access them di-
rectly. However, this approach defeats the purpose of encapsulating the class by revealing
its internal representation to the user. We can achieve encapsulation by making each of
these responsibilities be a method. If we later change the internal representation, the
methods can be rewritten to convert the new internal representation into the existing ex-
ternal form.
Would we ever want to declare a publicdata field in a class? Yes. Sometimes it’s useful
to provide constants that a collaborator can pass to responsibilities. For example, the names
of the primary colors for a Spectrumobject could be represented by public constants. The
constant REDcould be an intwith a value of 1 or a Stringwith a value of "RED". The collabo-
rator doesn’t know how the class represents the constant. It merely passes the class-supplied
constant as an argument to a method, as in this example:
colorObject.mixWith(RED);
In Java, such constants are declared static; that is, they belong to the class rather than
to an instance. We want every object of a given class to use the same set of constants, so they
should be kept centrally located in the class. The Phoneclass doesn’t have any such con-
stants on its CRC card. Normally, they would be identified during the scenario phase, when
we would note that some responsibilities receive one value from among a small set. Here’s
part of the CRC card for Spectrumthat illustrates the need for constants.
Class Name: Spectrum Superclass: Subclasses:
Responsibilities Collaborations
mixWith (a color, one of red, orange, yellow,
green, blue, indigo or violet)
.
.
.
None
T
E
A
M
F
L
Y
Team-Fly®
やまだぃちぅ
(やまだぃちぅ)
#1