Java Reference
In-Depth Information
Instead, youcreate these viewers inaseparate project, anddistribute their classfiles
only. Also, you design the application to enumerate its currently accessible viewers
whentheapplication startsrunning(perhapstheviewersarestoredinaJARfile),and
present this list to the user. When the user selects a specific viewer from this list, the
applicationloadstheviewer'sclassfileandinstantiatesthisclassviaits Class object.
The application can then invoke the object's methods.
Listing 4-11 presents the Viewer superclass that all viewer classes must extend.
Listing 4-11. Abstracting a viewer
abstract class Viewer
{
enum ViewMode { NORMAL, INFO, HEX };
abstract void view(byte[] content, ViewMode vm);
}
Viewer declares an enum to describe the three viewing modes. It also declares a
view() method that displays the content of its byte array argument according to the
viewer mode specified by its vm argument.
Listing 4-12 presents a Viewer subclass for viewing an EXE file's contents.
Listing 4-12. A viewer for viewing EXE content
class ViewerEXE extends Viewer
{
@Override
void view(byte[] content, ViewMode vm)
{
switch (vm)
{
case NORMAL:
System.out.println("outputting
EXE
content
normally");
break;
case INFO:
System.out.println("outputting EXE content in-
formationally");
break;
Search WWH ::




Custom Search