Datasheet
private Operation ops = new OpAdd();
private ResultWriter wtr = new ScreenWriter();
public static void main(String[] args) {
CalculateScreen calc = new CalculateScreen();
calc.execute(args);
}
public void execute(String [] args) {
long op1 = Long.parseLong(args[0]);
long op2 = Long.parseLong(args[1]);
wtr.showResult(“The result of “ + op1 +
ops.getOpsName() + op2 + “ is “
+ ops.operate(op1, op2) + “!”);
}
}
Mixing and Matching Components
If you need an application that multiplies two numbers and prints the result to a file, you can glue the
components together in the manner shown in the
CalculateMultFile class:
package com.wrox.begspring;
public class CalculateMultFile {
private Operation ops = new OpMultiply();
private ResultWriter wtr = new DataFileWriter();
public static void main(String[] args) {
CalculateMultFile calc = new CalculateMultFile();
calc.execute(args);
}
public void execute(String [] args) {
long op1 = Long.parseLong(args[0]);
long op2 = Long.parseLong(args[1]);
wtr.showResult(“The result of “ + op1 +
ops.getOpsName() + op2 + “ is “
+ ops.operate(op1, op2) + “!”);
}
}
Thus far, you have seen how to take a monolithic application and refactor it into components. You now
have a set of two interchangeable math components:
OpAdd and OpMultiply. There is also a set of two
interchangeable result writer components:
ScreenWriter and DataFileWriter.
You will see how the Spring framework can add construction flexibility very shortly. For now, take some
time to try out the following base application before proceeding further.
6
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 6










