Class 2 |
Intermediate Java 30-IT-397 |
|
Patterns Applied: An Advanced Topic
-
The challenge: write a program that reads files.
-
The program that reads the files is one class, called InsuranceFileReader.
-
There are, say, four different types of files. Let's call them Annuity,
Life, Car, and Homeowner.
-
Each of these four file types must correspond to a unique class.
Let's call the classes Annuity, Life, Car, and Homeowner.
-
If a new file type occurs, say, Business, we need to be able to read this
file from File Reader. But, we cannot make any changes to FileReader,
or any of the other classes we've already written.
-
Further, if tests are strictly not allowed.
-
Can we do this? Yes, with some help! Here's what we'll need:
-
Factory Pattern
-
Command Pattern
-
Class Class, methods forName() and newInstance()
-
A filename that identifies the type of class used to read the file.
-
A superclass for each of the file classes. Let's call it InputFile.
The Solution
Created by: Brandan Jones
December 17, 2001