来自:java编程思想
创建一个能够根据所传递的参数对象的不同而具有不同行为的方法,成为策略设计模式。
import java.util.Arrays;
class Processor{
public String name(){
return getClass().getSimpleName();
}
Object process(Object input){
return input;
}
}
class Upcase extends Processor{
String process(Object input){
return((String)input).toUpperCase();
}
}
class Downcase extends Processor{
String process(Object input){
return((String)input).toLowerCase();
}
}
class Splitter extends Processor{
String process(Object input){
return Arrays.toString(((String)input).split(" "));
}
}
public class StrategyTest {
public static void process(Processor p,Object s){
System.out.println("using process"+p.name());
System.out.println(p.process(s));
}
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);
}
}