Java Reference
In-Depth Information
public static void showVolume(Volume v)
{
System.out.println("I see ");
System.out.println(
"\t" + v.divide(GALLON) + " gallon(s),");
System.out.println(
"\t" + v.divide(CUBIC_INCH) + " cubic inch(es),");
System.out.println(
"\t" + v.divide(MILLILITER) + " milliliter(s).");
}
}
This program prints out:
I see:
0.75 gallon(s),
173.2 cubic inch(es),
2839.0 milliliter(s).
Many of the examples (and some of the challenges) that lie ahead use the
UnitConstants units to create and to apply various fireworks measurements.
Class and Object Adapters
The designs in Figures 3.1 and 3.2 are class adapters, which adapt through subclassing. When
you need to apply A DAPTER, you may not be able to subclass the class whose methods you
need to adapt. In some cases, you may need to create an adapter that adapts information from
more than one object. You may also find that the interface you need to adapt to is not a Java
interface but rather an abstract class that you must subclass. In such cases, you need to create
an object adapter —an adapter that uses delegation rather than subclassing.
A good example of a class that requires you to write an object adapter is the JTable class in
javax.swing . This class creates a GUI ( graphical user interface ) table component filled
with the information that your adapter feeds to it. To display data from your domain, JTable
provides constructors that accept an instance of the TableModel defined in
javax.swing.table and shown in Figure 3.4.
Search WWH ::




Custom Search