2015-09-07 11:44:36 -06:00
|
|
|
|
// interfaces/classprocessor/Apply.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-06-15 17:47:35 -07:00
|
|
|
|
package interfaces.classprocessor;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
class Processor {
|
|
|
|
|
public String name() {
|
|
|
|
|
return getClass().getSimpleName();
|
|
|
|
|
}
|
|
|
|
|
Object process(Object input) { return input; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Upcase extends Processor {
|
|
|
|
|
@Override
|
|
|
|
|
String process(Object input) { // Covariant return
|
|
|
|
|
return ((String)input).toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Downcase extends Processor {
|
|
|
|
|
@Override
|
|
|
|
|
String process(Object input) {
|
|
|
|
|
return ((String)input).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Splitter extends Processor {
|
|
|
|
|
@Override
|
|
|
|
|
String process(Object input) {
|
|
|
|
|
// The split() argument divides a String into pieces:
|
|
|
|
|
return Arrays.toString(((String)input).split(" "));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Apply {
|
|
|
|
|
public static void process(Processor p, Object s) {
|
2015-11-03 12:00:44 -08:00
|
|
|
|
System.out.println("Using Processor " + p.name());
|
|
|
|
|
System.out.println(p.process(s));
|
2015-06-15 17:47:35 -07:00
|
|
|
|
}
|
|
|
|
|
public static String s =
|
|
|
|
|
"Disagreement with beliefs is by definition incorrect";
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
process(new Upcase(), s);
|
|
|
|
|
process(new Downcase(), s);
|
|
|
|
|
process(new Splitter(), s);
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
Using Processor Upcase
|
|
|
|
|
DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT
|
|
|
|
|
Using Processor Downcase
|
|
|
|
|
disagreement with beliefs is by definition incorrect
|
|
|
|
|
Using Processor Splitter
|
|
|
|
|
[Disagreement, with, beliefs, is, by, definition,
|
|
|
|
|
incorrect]
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|