/*** Book: Thinking in Java * Features: Fully decoupled, policy design mode * Here the Apply.process () method can accept any type of processor, apply it to an object, and then print the result * File: apply.java* Time: April 2, 2015 16:50:55* Author: cutter_point*/package lesson9interfaces.classprocessor;import static Net.mindview.util.print.*;import java.util.arrays;class processor{public String name () {return This.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 Apply {public static void process (Processor p, Object s) {print ("Using Processor" + p.name ());p rint (P.process ( s));} public static String s = "I like China, I ' m Chinese, I can!"; public static void Main (String [] args) {Processor p = new Processor (); System.out.println (P.name ());p ROCESS (New Upcase (), s);p rocess (New Downcase (), s);p rocess (New Splitter (), s);}}
Results:
Processor
Using Processorupcase obj1
I like China, I ' M Chinese, I can! Obj1
Using Processordowncase obj1
I like China, I ' m Chinese, I can! Obj1
Using Processorsplitter obj1
[I, like, China,, I ' m, Chinese,, I, can!] Obj1
"Thinkinginjava" 10, fully decoupled, strategy design mode