Package Structure


You use packages to arbitrarily group your classes. This grouping of classes, known as the package structure, will change over time as your needs change. Initially, your concern will be ease of development. As the number of classes grows, you will want to create additional packages for manageability reasons. Once you deploy the application, your needs may change: You may want to organize the packages to increase the potential for reuse or perhaps to minimize maintenance impact to consumers of the package.

So far, your classes have all ended up in one package, studentinfo. A typical way to start organizing packages is to separate the user interfacethe part of the application that the end user interacts withfrom the underlying classes that represent business objects and infrastructural objects. The RosterReporter class in the previous example could be construed as part of the user interface, as it produces output that the end user will see.

Your next task will be to first move the studentinfo package down a level so that it is in a package named sis.reportinfo. You will then separate the RosterReporter and RosterReporterTest classes into their own package named report.

First create a new subdirectory named sis (for "Student Information System") at the same directory level as studentinfo. Beneath this directory, create a new subdirectory named report. Move the studentinfo subdirectory into the sis subdirectory. Move the RosterReporter and RosterReporterTest classes into the report subdirectory. Your directory structure should look something like:

 source    |-sis          |-studentinfo          |-report 

Next, you will change the package statements of all your classes. For the packages in the report subdirectory, use this package statement:

 package sis.report; 

For the packages in the studentinfo subdirectory, use this package statement:

 package sis.studentinfo; 

As you did in Lesson 2, remove all the class files (*.class), then recompile all your code. You will receive several errors. The problem is that the RosterReporter and RosterReporterTest classes are now in a separate package from the CourseSession and Student classes. They no longer have appropriate access to the classes in the other package.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net