Created
May 25, 2012 04:02
-
-
Save sanpingz/2785676 to your computer and use it in GitHub Desktop.
策略模式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static com.mceiba.util.Print.*; | |
import java.util.*; | |
interface Processor{ | |
String name(); | |
Object process(Object input); | |
} | |
class StringProcessor implements Processor{ | |
public String name(){ | |
return getClass().getSimpleName(); | |
} | |
public Object process(Object input) { return input; } | |
} | |
class Upcase extends StringProcessor{ | |
public String process(Object input){ //Covariant return | |
return ((String)input).toUpperCase(); | |
} | |
} | |
class Downcase extends StringProcessor{ | |
public String process(Object input){ //Covariant return | |
return ((String)input).toLowerCase(); | |
} | |
} | |
class Splitter extends StringProcessor{ | |
public String process(Object input){ //Covariant return | |
return Arrays.toString(((String)input).split(" ")); | |
} | |
} | |
public class Strategy{ | |
public static void process(Processor pro, Object obj){ | |
println("Using Processor "+pro.name()); | |
println(pro.process(obj)); | |
} | |
public static String str = "Beautiful is better than ugly"; | |
public static void main(String[] args){ | |
process(new Upcase(), str); | |
process(new Downcase(), str); | |
process(new Splitter(), str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment