Datasheet

void showResult(String result) ;
}
One implementation of ResultWriter, called ScreenWriter, writes to the console screen:
package com.wrox.begspring;
public class ScreenWriter implements ResultWriter{
public ScreenWriter() {}
public void showResult(String result) {
System.out.println(result);
}
}
Another implementation of ResultWriter, called DataFileWriter, writes the result to a file:
package com.wrox.begspring;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
public class DataFileWriter implements ResultWriter {
public DataFileWriter() {}
public void showResult(String result) {
File file = new File(“output.txt”);
try {
PrintWriter fwriter = new PrintWriter(
new BufferedWriter(new FileWriter(file)));
fwriter.println(result);
fwriter.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Putting the Application Together
With the Operation and ResultWriter implementations factored out as reusable components, it is
possible to glue a selection of the components together to create an application that adds two numbers
and prints the result to the screen. This is done in the
CalculateScreen class:
package com.wrox.begspring;
public class CalculateScreen {
5
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 5