diff --git a/celuemoshijingjian/.idea/.gitignore b/celuemoshijingjian/.idea/.gitignore new file mode 100644 index 0000000..b03b3fc --- /dev/null +++ b/celuemoshijingjian/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../:\java project\celuemoshijingjian\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/celuemoshijingjian/.idea/compiler.xml b/celuemoshijingjian/.idea/compiler.xml new file mode 100644 index 0000000..8feeb0f --- /dev/null +++ b/celuemoshijingjian/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/celuemoshijingjian/.idea/jarRepositories.xml b/celuemoshijingjian/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/celuemoshijingjian/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/celuemoshijingjian/.idea/misc.xml b/celuemoshijingjian/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/celuemoshijingjian/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/celuemoshijingjian/celuemoshijingjian.iml b/celuemoshijingjian/celuemoshijingjian.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/celuemoshijingjian/celuemoshijingjian.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/celuemoshijingjian/pom.xml b/celuemoshijingjian/pom.xml new file mode 100644 index 0000000..1543292 --- /dev/null +++ b/celuemoshijingjian/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.woniu + celuemoshijingjian + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/FunctionStrategy.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/FunctionStrategy.java new file mode 100644 index 0000000..c8ef9d9 --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/FunctionStrategy.java @@ -0,0 +1,28 @@ +package com.woniu.celuejj; + +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * 用java 8的函数式接口和泛型 优化策略模式(简化写法) + * 进而优化if else逻辑 + * @className: FunctionStrategy + * @author: woniu + * @date: 2023/2/11 + **/ +public interface FunctionStrategy { + + /** + * 暴露当前策略的钩子 + * + * @return 判断钩子 + */ + Predicate

predicate(); + + /** + * 暴露当前策略的生产逻辑 + * + * @return 消费逻辑 + */ + Function function(); +} diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/ReturnHomeStrategy.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/ReturnHomeStrategy.java new file mode 100644 index 0000000..52d132e --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/ReturnHomeStrategy.java @@ -0,0 +1,56 @@ +package com.woniu.celuejj; + +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * 策略模式简化写法 来优化if else逻辑 + * String 代表来自区域风险系数 + * Integer 打工人 + * String 村里开具证明 + */ +enum ReturnHomeStrategy implements FunctionStrategy { + + /** + * 高风险 + */ + HIGH_RISK(from -> "HIGH_RISK".equals(from), i -> i+"在高风险回来了,get out!"), + /** + * 中风险 + */ + MIDDLE_RISK(from -> "MIDDLE_RISK".equals(from), i -> i+"在中风险回来了,get out!"), + /** + * 低风险 + */ + LOW_RISK(from -> "LOW_RISK".equals(from), i -> i+"在低风险回来了,拉走先做个核酸检测!"); + + private final Predicate predicate; + + private final Function function; + + public Predicate getPredicate() { + return predicate; + } + + public Function getFunction() { + return function; + } + + ReturnHomeStrategy(Predicate predicate, Function function) { + this.predicate = predicate; + this.function = function; + } + + @Override + public Predicate predicate() { + return this.predicate; + } + + @Override + public Function function() { + return this.function; + } + + + +} diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/TestMain.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/TestMain.java new file mode 100644 index 0000000..600bfbe --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/TestMain.java @@ -0,0 +1,29 @@ +package com.woniu.celuejj; + +/** + * 策略模式简化写法 来优化if else逻辑 + */ +public class TestMain { + + + public static void main(String[] args) { + String low_risk = returnHome("LOW_RISK", 1); + System.out.println(low_risk); + + } + + /** + * 某人回村 + * @param from 代表区域风险系数 + * @param id 打工人 + * @return 要证明 + */ + public static String returnHome(String from, Integer id) { + for (ReturnHomeStrategy value : ReturnHomeStrategy.values()) { + if (value.predicate().test(from)) { + return value.function().apply(id); + } + } + throw new RuntimeException("外星人,抓起来放进动物园卖门票!"); + } +} diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/FunctionStrategy.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/FunctionStrategy.java new file mode 100644 index 0000000..a1237de --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/FunctionStrategy.java @@ -0,0 +1,14 @@ +package com.woniu.celuejj.celueyuanxing; + +/** + * 用java 8的函数式接口和泛型 优化策略模式(简化写法) + * 进而优化if else逻辑 + * 疫情期间 某人回村为例 + * @className: FunctionStrategy + * @author: woniu + * @date: 2023/2/11 + **/ +public interface FunctionStrategy { + + void huicun(); +} diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/HighRiskFunctionStrategy.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/HighRiskFunctionStrategy.java new file mode 100644 index 0000000..90f3598 --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/HighRiskFunctionStrategy.java @@ -0,0 +1,10 @@ +package com.woniu.celuejj.celueyuanxing; + + +public class HighRiskFunctionStrategy implements FunctionStrategy { + + @Override + public void huicun() { + System.out.println("在高风险回来了,get out!"); + } +} diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/LowRiskStrategy.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/LowRiskStrategy.java new file mode 100644 index 0000000..46906b3 --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/LowRiskStrategy.java @@ -0,0 +1,10 @@ +package com.woniu.celuejj.celueyuanxing; + +public class LowRiskStrategy implements FunctionStrategy { + + + @Override + public void huicun() { + System.out.println("在低风险回来了,先做个核酸吧!"); + } +} diff --git a/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/MiddleRiskStrategy.java b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/MiddleRiskStrategy.java new file mode 100644 index 0000000..c8c8a8b --- /dev/null +++ b/celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/MiddleRiskStrategy.java @@ -0,0 +1,9 @@ +package com.woniu.celuejj.celueyuanxing; + + +public class MiddleRiskStrategy implements FunctionStrategy { + @Override + public void huicun() { + System.out.println("在中风险回来了,get out!"); + } +} diff --git a/celuemoshijingjian/target/classes/com/woniu/celuejj/FunctionStrategy.class b/celuemoshijingjian/target/classes/com/woniu/celuejj/FunctionStrategy.class new file mode 100644 index 0000000..e4bebcc Binary files /dev/null and b/celuemoshijingjian/target/classes/com/woniu/celuejj/FunctionStrategy.class differ diff --git a/celuemoshijingjian/target/classes/com/woniu/celuejj/ReturnHomeStrategy.class b/celuemoshijingjian/target/classes/com/woniu/celuejj/ReturnHomeStrategy.class new file mode 100644 index 0000000..93d9525 Binary files /dev/null and b/celuemoshijingjian/target/classes/com/woniu/celuejj/ReturnHomeStrategy.class differ