import java.util.HashMap;import java.util.Map;public class Test {static Map<String, Operate> operates = new HashMap<String, Operate>();static {operates.put("upper", new UpperOperate());operates.put("lower", new LowerOperate());operates.put("reverse", new ReverseOperate());operates.put("removeBlank", new RemoveBlankOperate());}public static void main(String[] args) {String operateType = "removeBlank";String content = "Love coding!";Operate operate = operates.get(operateType);if (operate != null) {String result = operate.handle(content);System.out.println(result);}}}interface Operate {public String handle(String content);}/** * 大寫操作 */class UpperOperate implements Operate {@Overridepublic String handle(String content) {if (content == null) {return content;}return content.toUpperCase();}}/** * 小寫操作 */class LowerOperate implements Operate {@Overridepublic String handle(String content) {if (content == null) {return content;}return content.toLowerCase();}}/** * 逆序操作 */class ReverseOperate implements Operate {@Overridepublic String handle(String content) {if (content == null) {return content;}return new StringBuffer(content).reverse().toString();}}/** * 去除空白操作 */class RemoveBlankOperate implements Operate {@Overridepublic String handle(String content) {if (content == null) {return content;}return content.replaceAll("\\s*|\t|\r|\n", "");}}