Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^307


What are the responsibilities of the driver? To create instances of the class Entryand
write them to a file.Entryhas a responsibility that writes itself to a file, so that respon-
sibility is implemented by sending a message to Entry. We have two possibilities for cre-
ating instances of the class: We can use the nonparameterized version of the
constructor for Entryand let it be responsible for instantiating Name,Address, and Phone,
or we can let the driver input the values and use Entry’s parameterized constructor.
Because this is a special-purpose driver whose only function is to create entries, let’s
centralize the input for the fields of Entryin the driver. We should also use the parame-
terized constructors for Nameand Address.Phonedoesn’t have a nonparameterized
constructor, so there is no choice. Here, then, is the CRC card for the driver:


Class Name: AddressDr Superclass: Subclasses:


Responsibilities Collaborations
Prepare input file BufferedReader

Prepare output file PrintWriter
Process entries Name, Address, Phone, Entry
Close files BufferedReader, PrintWriter

Class Name: Address Superclass: Subclasses:


Responsibilities Collaborations
Create (street, city, state, zip) String

Create( ) String. BufferedReader


Know street None
return String

Know city None
return String

return String


Know state None


Know zip None


return String

Free download pdf