"Thinkinginjava" 10, fully decoupled, strategy design mode

Source: Internet
Author: User

/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.