代码如下:
/** * java8 函数式编程实现策略模式 * * @date 2022/11/4 */ public class FunctionStrategy { private static Map<String, Function<String, String>> functionMap = new HashMap<>(); static { BankBusiness bankBusiness = new BankBusiness(); functionMap.put("one", bankBusiness::one); functionMap.put("two", bankBusiness::two); functionMap.put("three", bankBusiness::three); } public String getCheckResult(String key, String str) { Function<String, String> function = functionMap.get(key); if (function != null) { return function.apply(str); } return "error"; } public static void main(String[] args) { FunctionStrategy functionStrategy = new FunctionStrategy(); System.out.println(functionStrategy.getCheckResult("one", "来咯")); } //内部类 private static class BankBusiness { public String one(String str) { return "第一次执行" + str; } public String two(String str) { return "第二次执行" + str; } public String three(String str) { return "第三次执行" + str; } } }
执行结果:
标签:functionMap,return,String,bankBusiness,编程,java8,str,public,函数 From: https://www.cnblogs.com/xjwly/p/16858677.html