标签:style blog http io color ar os java sp
1 策略设计模式
1 package thinkJava; 2 /* 3 * 策略设计模式 4 * 像本例一样,能够根据所传递的参数对象的不同而具有不同的行为的方法。 5 * Processor对象就是一个策略,有3中不同的策略应用到了String类型的对象上。 6 */ 7 import java.util.Arrays; 8 9 class Processor{ 10 public String name(){ 11 return getClass().getSimpleName(); 12 } 13 Object process(Object input){ 14 return input; 15 } 16 } 17 class Upcase extends Processor{ 18 String process(Object input){ 19 return ((String)input).toUpperCase(); 20 } 21 } 22 class Downcase extends Processor{ 23 String process(Object input){ 24 return ((String)input).toLowerCase(); 25 } 26 } 27 class Splitter extends Processor{ 28 String process(Object input){ 29 return Arrays.toString(((String)input).split(" ")); 30 } 31 } 32 public class Apply { 33 34 public static void process(Processor p, Object s){ 35 System.out.println("Using Processor " + p.name()); 36 System.out.println(p.process(s)); 37 } 38 public static String s = "Disagreemment with beliefs is by definition incorrect"; 39 public static void main(String[] args){ 40 process(new Upcase(), s); 41 process(new Downcase(), s); 42 process(new Splitter(), s); 43 } 44 45 }
运行结果
标签:style blog http io color ar os java sp
原文地址:http://www.cnblogs.com/hdu-2010/p/4091766.html