From ac44d08cedd998602991407d6673029e85d7832f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Sat, 11 Feb 2023 19:32:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AD=96=E7=95=A5=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- celuemoshijingjian/.idea/.gitignore | 8 +++ celuemoshijingjian/.idea/compiler.xml | 13 ++++ celuemoshijingjian/.idea/jarRepositories.xml | 20 +++++++ celuemoshijingjian/.idea/misc.xml | 14 +++++ celuemoshijingjian/celuemoshijingjian.iml | 2 + celuemoshijingjian/pom.xml | 12 ++++ .../com/woniu/celuejj/FunctionStrategy.java | 28 +++++++++ .../com/woniu/celuejj/ReturnHomeStrategy.java | 56 ++++++++++++++++++ .../main/java/com/woniu/celuejj/TestMain.java | 29 +++++++++ .../celueyuanxing/FunctionStrategy.java | 14 +++++ .../HighRiskFunctionStrategy.java | 10 ++++ .../celueyuanxing/LowRiskStrategy.java | 10 ++++ .../celueyuanxing/MiddleRiskStrategy.java | 9 +++ .../com/woniu/celuejj/FunctionStrategy.class | Bin 0 -> 440 bytes .../woniu/celuejj/ReturnHomeStrategy.class | Bin 0 -> 4902 bytes 15 files changed, 225 insertions(+) create mode 100644 celuemoshijingjian/.idea/.gitignore create mode 100644 celuemoshijingjian/.idea/compiler.xml create mode 100644 celuemoshijingjian/.idea/jarRepositories.xml create mode 100644 celuemoshijingjian/.idea/misc.xml create mode 100644 celuemoshijingjian/celuemoshijingjian.iml create mode 100644 celuemoshijingjian/pom.xml create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/FunctionStrategy.java create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/ReturnHomeStrategy.java create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/TestMain.java create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/FunctionStrategy.java create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/HighRiskFunctionStrategy.java create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/LowRiskStrategy.java create mode 100644 celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/MiddleRiskStrategy.java create mode 100644 celuemoshijingjian/target/classes/com/woniu/celuejj/FunctionStrategy.class create mode 100644 celuemoshijingjian/target/classes/com/woniu/celuejj/ReturnHomeStrategy.class 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 0000000000000000000000000000000000000000..e4bebccf6d8029a330d5d19c26b10c3b29021caa GIT binary patch literal 440 zcmaiw%}&EG5QJw-Y1;C8;Y@{ufD68GB2kY$ArUox0oRzwQE*UXgLpL#JOB@cx=jf} z5Vgk{f3x15&#(6n05>>_a1h~;FnX@dgVo9#!sPPmu23&Z1aC|67@YRDaw2VsnDNZ! zPWezXe!c}yWExL7Gi)ajPP-$s@2?oX*i0l+#&X__O_TI?y>6*oF76kF(LRPTH+9UvoYA+x4m&!kMm~#B1ej5ZaW%6ovRp5{{c5e=Rm35PIA_ c(CZU+&}Y57QW&s~F#ORY_SP?B>@~#x5~f;y8UO$Q literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..93d95251b9dc84c56d0a70338a729e73c9b0053e GIT binary patch literal 4902 zcma)A`+pSG75`4MyR(^$uvwDOAV{!5HV;@HfdWY*5`tOXfU<~<*wV>n5+>Q5S$AiF z^r=;>fKsi1AigS6qX>myk|oHa@7CJK5B(EJBtP}n{$zXZ%bab;$J0jK@rf0ejb~7!~`@T&V(u(G&W6mu|~i%ND3rLl|V;YzyTa& z(?e_+D8&!(L-zKgQv4V{;qg-*&wBBkfO7nd$MXUf;srJw_TokM{5cz560iVAyg2H` zYA=p?vC2^WY6XikPezC)xB(KMgc>o71;>yu;&N4khiOhuin<3Pm2} z@IW}K#e)YmRY?bFBo*4tZcr#*=9DWg zDJuCE*lQx%kD0M9C3`863mlf|492vB`;tnkpM#Lh^$HG~9N4qLXMM7&$i5{9w^8Hd zSWi-mb0|`}X?F)>4);WKDcav5C9DdbACux;-BL|Tmvkjsv!2eW-H(F1atfWd>Gq8r zD(v|8Y`PafK*6l@XVwd4&5%1ww9BSrrpNM!6?~7QA zbs{$4TOuCCIUXM{@p%!u@dS@wi}(m_6YU1>}cgDsVCXT*-cX)K-?eh~SZr;6h zcKqnzmq$JtKYMZfl~a>%41Rfja2fOO0x>WJXp*!wt;D+JBwLn64B;wWYU6JW-nnsn z^4#&sGX(Mu!QL5rv0>uak-Ni}#}B_W{@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