Programming and Problem Solving with Java

(やまだぃちぅ) #1
6.6 Object-Oriented Implementation | 289

6.6 Object-Oriented Implementation


Our design is now represented in a set of CRC cards. In general, each CRC card becomes a Java
class and the responsibilities become methods of the class. This translation begins by re-
placing the expressions used to describe a responsibility with properly formed method
names. If the responsibility returns a value, the method should be a value-returning method.
If the responsibility requires information, the information becomes a parameter in the
method. This information is available on the CRC card.
The next step is to write the algorithms to implement the responsibilities. In many cases
the algorithms are very simple, requiring only the return of a value. In other cases the algo-
rithms are more complex, requiring a further functional design.


Class Interface Design


Let’s begin our discussion of designing the class interface by looking at the CRC card for a
new class, Phone, with two fields: one for the area code and one for the digits in the number.


We know that “Create itself” must be a constructor that uses newto instantiate itself. The
remaining responsibilities all return information about a phone number, so they are observers.


Class Name: Phone Superclass: Subclasses:


Responsibilities Collaborations
Create itself (areaCode, digits) None

Know area code None


return int
Know digits None

return int


Number as digits


Are two numbers equal?


None


return int


String


return boolean Phone


return int


Number in print form None

Free download pdf