Datasheet
mathematic operation, and from the writer’s destination, by means of Java interfaces. Two interfaces are
defined. The first, called
Operation, encapsulates the mathematic operation:
package com.wrox.begspring;
public interface Operation {
long operate(long op1, long op2);
String getOpsName();
}
Decoupling at the Interface
Next, a component that performs addition can be written as the OpAdd class:
package com.wrox.begspring;
public class OpAdd implements Operation{
public OpAdd() {}
public String getOpsName() {
return “ plus “;
}
public long operate(long op1, long op2) {
return op1 + op2;
}
}
And another component that performs multiplication can be written as OpMultiply:
package com.wrox.begspring;
public class OpMultiply implements Operation {
public OpMultiply() {}
public String getOpsName() {
return “ times “;
}
public long operate(long op1, long op2) {
return op1 * op2;
}
}
Note that this refactoring creates two components that can be reused in other applications.
Creating the Result Writer Components
In a similar way, the writing of the result either to the screen or to a file can be decoupled via a
ResultWriter interface:
package com.wrox.begspring;
public interface ResultWriter {
4
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 4










