From fc727ea8e9043d18a3b41d9326ae2c98a2cfa195 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: Mon, 19 Dec 2022 23:41:29 +0800 Subject: [PATCH] demo --- demodump/.idea/.gitignore | 8 ++ demodump/.idea/compiler.xml | 14 ++ demodump/.idea/jarRepositories.xml | 20 +++ demodump/.idea/misc.xml | 14 ++ demodump/.idea/setting.xml | 8 ++ demodump/.idea/uiDesigner.xml | 124 ++++++++++++++++++ demodump/demodump.iml | 2 + demodump/pom.xml | 29 ++++ .../org/example/function/BranchHandle.java | 19 +++ .../function/PresentOrElseHandler.java | 25 ++++ .../function/ThrowExceptionFunction.java | 17 +++ .../java/org/example/function/VUtils.java | 63 +++++++++ .../java/org/example/strategy/Eclipse.java | 19 +++ .../main/java/org/example/strategy/IDEA.java | 17 +++ .../java/org/example/strategy/Program.java | 16 +++ .../java/org/example/strategy/Programmer.java | 57 ++++++++ demodump/src/test/java/org/example/Test.java | 53 ++++++++ .../org/example/function/BranchHandle.class | Bin 0 -> 200 bytes .../function/PresentOrElseHandler.class | Bin 0 -> 359 bytes .../function/ThrowExceptionFunction.class | Bin 0 -> 193 bytes .../classes/org/example/function/VUtils.class | Bin 0 -> 2412 bytes .../org/example/strategy/Eclipse.class | Bin 0 -> 562 bytes .../classes/org/example/strategy/IDEA.class | Bin 0 -> 544 bytes .../org/example/strategy/Program.class | Bin 0 -> 139 bytes .../org/example/strategy/Programmer.class | Bin 0 -> 1070 bytes .../test-classes/org/example/Test.class | Bin 0 -> 1561 bytes 26 files changed, 505 insertions(+) create mode 100644 demodump/.idea/.gitignore create mode 100644 demodump/.idea/compiler.xml create mode 100644 demodump/.idea/jarRepositories.xml create mode 100644 demodump/.idea/misc.xml create mode 100644 demodump/.idea/setting.xml create mode 100644 demodump/.idea/uiDesigner.xml create mode 100644 demodump/demodump.iml create mode 100644 demodump/pom.xml create mode 100644 demodump/src/main/java/org/example/function/BranchHandle.java create mode 100644 demodump/src/main/java/org/example/function/PresentOrElseHandler.java create mode 100644 demodump/src/main/java/org/example/function/ThrowExceptionFunction.java create mode 100644 demodump/src/main/java/org/example/function/VUtils.java create mode 100644 demodump/src/main/java/org/example/strategy/Eclipse.java create mode 100644 demodump/src/main/java/org/example/strategy/IDEA.java create mode 100644 demodump/src/main/java/org/example/strategy/Program.java create mode 100644 demodump/src/main/java/org/example/strategy/Programmer.java create mode 100644 demodump/src/test/java/org/example/Test.java create mode 100644 demodump/target/classes/org/example/function/BranchHandle.class create mode 100644 demodump/target/classes/org/example/function/PresentOrElseHandler.class create mode 100644 demodump/target/classes/org/example/function/ThrowExceptionFunction.class create mode 100644 demodump/target/classes/org/example/function/VUtils.class create mode 100644 demodump/target/classes/org/example/strategy/Eclipse.class create mode 100644 demodump/target/classes/org/example/strategy/IDEA.class create mode 100644 demodump/target/classes/org/example/strategy/Program.class create mode 100644 demodump/target/classes/org/example/strategy/Programmer.class create mode 100644 demodump/target/test-classes/org/example/Test.class diff --git a/demodump/.idea/.gitignore b/demodump/.idea/.gitignore new file mode 100644 index 0000000..aed7f1f --- /dev/null +++ b/demodump/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../:\demodump\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/demodump/.idea/compiler.xml b/demodump/.idea/compiler.xml new file mode 100644 index 0000000..dfd2b09 --- /dev/null +++ b/demodump/.idea/compiler.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demodump/.idea/jarRepositories.xml b/demodump/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/demodump/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/demodump/.idea/misc.xml b/demodump/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/demodump/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/demodump/.idea/setting.xml b/demodump/.idea/setting.xml new file mode 100644 index 0000000..ed05fd8 --- /dev/null +++ b/demodump/.idea/setting.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/demodump/.idea/uiDesigner.xml b/demodump/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/demodump/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demodump/demodump.iml b/demodump/demodump.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/demodump/demodump.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/demodump/pom.xml b/demodump/pom.xml new file mode 100644 index 0000000..8a20857 --- /dev/null +++ b/demodump/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + org.example + demodump + 1.0-SNAPSHOT + + + + org.springframework + spring-context + 5.3.23 + + + + + junit + junit + 4.13 + test + + + + + + \ No newline at end of file diff --git a/demodump/src/main/java/org/example/function/BranchHandle.java b/demodump/src/main/java/org/example/function/BranchHandle.java new file mode 100644 index 0000000..28737bb --- /dev/null +++ b/demodump/src/main/java/org/example/function/BranchHandle.java @@ -0,0 +1,19 @@ +package org.example.function; + +/** + * @className: BranchHandle + * @author: xiaomage + * @date: 2022/12/17 + **/ +public interface BranchHandle { + + /** + * 分支操作 + * + * @param trueHandle 为true时要进行的操作 + * @param falseHandle 为false时要进行的操作 + * @return void + **/ + void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle); + +} diff --git a/demodump/src/main/java/org/example/function/PresentOrElseHandler.java b/demodump/src/main/java/org/example/function/PresentOrElseHandler.java new file mode 100644 index 0000000..04113f6 --- /dev/null +++ b/demodump/src/main/java/org/example/function/PresentOrElseHandler.java @@ -0,0 +1,25 @@ +package org.example.function; + +/** + * @className: PresentOrElseHandler + * @author: xiaomage + * @date: 2022/12/17 + **/ + +import java.util.function.Consumer; + +/** + * 空值与非空值分支处理 + */ +public interface PresentOrElseHandler { + + /** + * 值不为空时执行消费操作 + * 值为空时执行其他的操作 + * + * @param action 值不为空时,执行的消费操作 + * @param emptyAction 值为空时,执行的操作 + **/ + void presentOrElseHandle(Consumer action, Runnable emptyAction); + +} \ No newline at end of file diff --git a/demodump/src/main/java/org/example/function/ThrowExceptionFunction.java b/demodump/src/main/java/org/example/function/ThrowExceptionFunction.java new file mode 100644 index 0000000..045adc0 --- /dev/null +++ b/demodump/src/main/java/org/example/function/ThrowExceptionFunction.java @@ -0,0 +1,17 @@ +package org.example.function; + +/** + * @className: ThrowExceptionFunction + * @author: xiaomage + * @date: 2022/12/17 + **/ +public interface ThrowExceptionFunction { + + /** + * 抛出异常信息 + * + * @param message 异常信息 + * @return void + **/ + void throwMessage(String message); +} diff --git a/demodump/src/main/java/org/example/function/VUtils.java b/demodump/src/main/java/org/example/function/VUtils.java new file mode 100644 index 0000000..8b6c3df --- /dev/null +++ b/demodump/src/main/java/org/example/function/VUtils.java @@ -0,0 +1,63 @@ +package org.example.function; + +/** + * vutils + * + * @author Admin + * @date 2022/12/17 + */ +public class VUtils { + + /** + * 如果参数为true抛出异常 + * + * @param b + * @return com.example.demo.func.ThrowExceptionFunction + **/ + public static ThrowExceptionFunction isTure(boolean b) { + + return (errorMessage) -> { + if (b) { + throw new RuntimeException(errorMessage); + } + }; + } + + + /** + * 参数为true或false时,分别进行不同的操作 + * + * @param b + * @return com.example.demo.func.BranchHandle + **/ + public static BranchHandle isTureOrFalse(boolean b){ + + return (trueHandle, falseHandle) -> { + if (b){ + trueHandle.run(); + } else { + falseHandle.run(); + } + }; + } + + + /** + * 参数为true或false时,分别进行不同的操作 + * + * @param b + * @return com.example.demo.func.BranchHandle + **/ + public static PresentOrElseHandler isBlankOrNoBlank(String str){ + + return (consumer, runnable) -> { + if (str == null || str.length() == 0){ + runnable.run(); + } else { + consumer.accept(str); + } + }; + } + + +} diff --git a/demodump/src/main/java/org/example/strategy/Eclipse.java b/demodump/src/main/java/org/example/strategy/Eclipse.java new file mode 100644 index 0000000..04041a9 --- /dev/null +++ b/demodump/src/main/java/org/example/strategy/Eclipse.java @@ -0,0 +1,19 @@ +package org.example.strategy; + +/** + * Eclipse + * + * @author xiaomage + * @date 2022/12/18 + */ +public class Eclipse implements Program { + + + /** + * 编码 + */ + @Override + public void coding() { + System.out.println("用Eclipse 进行编码"); + } +} diff --git a/demodump/src/main/java/org/example/strategy/IDEA.java b/demodump/src/main/java/org/example/strategy/IDEA.java new file mode 100644 index 0000000..9d4a67a --- /dev/null +++ b/demodump/src/main/java/org/example/strategy/IDEA.java @@ -0,0 +1,17 @@ +package org.example.strategy; + + +/** + * IDEA + * + * @author xiaomage + * @date 2022/12/18 + */ +public class IDEA implements Program { + + + @Override + public void coding() { + System.out.println("用IDEA 编码"); + } +} diff --git a/demodump/src/main/java/org/example/strategy/Program.java b/demodump/src/main/java/org/example/strategy/Program.java new file mode 100644 index 0000000..cbf965e --- /dev/null +++ b/demodump/src/main/java/org/example/strategy/Program.java @@ -0,0 +1,16 @@ +package org.example.strategy; + + +/** + * 编程 + * + * @author xiaomage + * @date 2022/12/18 + */ +public interface Program { + + /** + * 编码 + */ + void coding(); +} diff --git a/demodump/src/main/java/org/example/strategy/Programmer.java b/demodump/src/main/java/org/example/strategy/Programmer.java new file mode 100644 index 0000000..ebcc107 --- /dev/null +++ b/demodump/src/main/java/org/example/strategy/Programmer.java @@ -0,0 +1,57 @@ +package org.example.strategy; + +import java.util.Objects; + +/** + * 程序员 + * 分享用策略模式去消除代码中的if else + * @author xiaomage + * @date 2022/12/18 + */ +public class Programmer { + + private Program program; + + public Programmer(Program program) { + this.program = program; + } + + public Program getProgram() { + return program; + } + + public void setProgram(Program program) { + this.program = program; + } + +// /** +// * 编码 +// * +// * @param codeType 类型 +// */ +// public void coding(String codeType) { +// if (Objects.equals("IDEA",codeType)) { +// System.out.println("用IDEA编码"); +// }else if(Objects.equals("Eclipse",codeType)){ +// System.out.println("用Eclipse编码"); +// } +//// ...... +// } + + /** + * 用策略模式编码 + */ + public void codingStrategy( ) { + program.coding(); + } + + public static void main(String[] args) { + Programmer programmerI = new Programmer(new IDEA()); + programmerI.codingStrategy(); + + Programmer programmerE = new Programmer(new Eclipse()); + programmerE.codingStrategy(); + } + + +} diff --git a/demodump/src/test/java/org/example/Test.java b/demodump/src/test/java/org/example/Test.java new file mode 100644 index 0000000..7df44b9 --- /dev/null +++ b/demodump/src/test/java/org/example/Test.java @@ -0,0 +1,53 @@ +package org.example; + +import org.example.function.VUtils; + +/** + * 用Java 8中的 Function接口,消灭代码中if...else + * + * Supplier供给型函数 + * Consumer消费型函数 + * Runnable无参无返回型函数 + * Function函数的表现形式为接收一个参数,并返回一个值。 + * Supplier、Consumer和Runnable可以看作Function的一种特殊表现形式 + * + * 处理抛出异常的if + * 处理if分支操作 + * 如果存在值执行消费操作,否则执行基于空的操作 + */ +public class Test { + + + public static void main(String[] args) { +// if (...){ +// throw new RuntimeException("出现异常了"); +// } +// +// if (...){ +// doSomething(); +// } else { +// doOther(); +// } + +// VUtils.isTure(true).throwMessage("hahaha"); + + +// VUtils.isTureOrFalse(false) +// .trueOrFalseHandle( +// () -> { +// System.out.println("我是true ,现在我要这样做"); +// }, +// () -> { +// System.out.println("我是false ,天下无敌"); +// } +// ); + + VUtils.isBlankOrNoBlank("hello") + .presentOrElseHandle(System.out::println, + () -> { + System.out.println("空字符串"); + }); + + + } +} diff --git a/demodump/target/classes/org/example/function/BranchHandle.class b/demodump/target/classes/org/example/function/BranchHandle.class new file mode 100644 index 0000000000000000000000000000000000000000..18e6d276a4463c65c72b5992f9805cda4d1e01f1 GIT binary patch literal 200 zcmX^0Z`VEs1_l!bPId-%b_Nbc2Eme|(p3Kxyu8GuoK$OUVwz!$3|ztarA5i9ZkZshf=)$=dC3_Nlk`BQFfu6S7p3c`RwU*Y zrflFa-(edqkV;?msIB5R0bPGVlVeo$#%USd*Cs)VFnv>`2l5Id zgJynFx_)X!Vs1fBD#B0LG%zv;t)P5@nqY3l$0 literal 0 HcmV?d00001 diff --git a/demodump/target/classes/org/example/function/ThrowExceptionFunction.class b/demodump/target/classes/org/example/function/ThrowExceptionFunction.class new file mode 100644 index 0000000000000000000000000000000000000000..b81ae957fbd600203c4fdc2af407f4e9ec94deff GIT binary patch literal 193 zcmX^0Z`VEs1_l!bPId-%b_Nbc2A-0PqWp5-)Z*gA^i)O$Q4OE0#Ii*FoW#6z{os%T$e^ORzatRwF10w?y(2)!bj4X@{%nU3b76U7gWMW_g0J*L< AdH?_b literal 0 HcmV?d00001 diff --git a/demodump/target/classes/org/example/function/VUtils.class b/demodump/target/classes/org/example/function/VUtils.class new file mode 100644 index 0000000000000000000000000000000000000000..23e1e4ee391c8c881b21d5b9d64964744eeeb71a GIT binary patch literal 2412 zcmbtWT~pge6g`Upfh@oQL&8VWG%*Pn8bcbIw5Ftm;)d2h8WY;kPuU1tL6%%ePLrR~ z{(-)>FUf??v~PXr^fz^SRub68hUrWntX-}4o^$uyb0z=v&(ZGy?!ZtHM`l>ghaqrx z7+rWzMHb6(ypNSQ?&AXmAFAlXs)98I9|`m>o3`n%2*hS)cLloFoP9%JC~w-vR=rv@ z+#S7Wab-B~lyqxXcTIU8Eq41A(-XLycieKuc&=A#mXSHA+a=$0?9A?C-?Y3eIZSV- z?h<5SW^cCh-kpl;{ILGKWYlD6E`ngJDDt-_Fc4yFyE)zR2zBF3s8v_DOO*$@y>A%= zNSNNLrQ3(w?v@iw1n$q|pX$%_442E9g72DkIeSK44_(7EY=7HbCu@l55;W%duGFLr zMxei7mTlb+&b#{mh?dhU)uz2cF1Z5r#b_GTiqy7AlV6ywJP{}>2Oo8MK&2bIf1l6RKnDv!2HZ!CxlLG zGD+2U>roK|;s??QVM%D%7K+q@z*r0Rag~Xp8LsQNn}+AICZyVcx5jISwCdcz|RA8=QZl;Zs~nAkX=xhAm7e*w*k6 zpDB2xp@0DeI~pE?8B8>6!y@=*)i`m!B+Us-p3_iFsS&?jd}@?@6`$h^fzb|bnB7&O z;t7xK%N9F1_FVJc6DxF9ULvpsFG%^L>K*HTCxSV{Eob9>DKJuNZ?2ZZg+=50!!qo$ zUkTn(8*IDA@sAVdBf65h1&=x>Lma&%UofYH3MFJ0DELal*I?n(nA30rH#NM0s{+>= zgr@z>IW#i)pe1Pt{Xj4Ij{AbvsW$Eul*pEMoWpvJ%5T`V;jUS_=NWuk{@tv6gcn*X z6xBAa*tb%=k={9{jok!rx`TOuD zeHQ1}3sCs^V)yYn$h!p1m{|~0T&=YfBhIik}EQJk)sS& zCXN_~UpQ?JA886#Fp9At{30%O2#>XgU+)+`jtP>qu&0mE-5$Qw5#KMI#T4A=4uaO@xETtvjHOd3P(wwocIyiT*TWSCUBZIMe=Eu?j~z6 z+eDj;#3c%5NG5BNT!WbBF=F0t?$!6ITKS_Ks%={M(cuG}N+ZA43ZOR=%h&Q=Qr z5$*jW$*&OSlHdc>W`_CZ`@Y%Pd3%3)0kDcC6NAX5k;nKD3MM8{G%#sk%D^X;=5Yh%_EX-n#q1e8-?Y9>@ zuUDO$^Y-&)`})klyoCiUGR*!rlOYqE+8z&Fd$(~U9Q~JKPlc}H{trd%G}3~gWY{8I zq1yG5p}nVMplezQ?$cKJUtk8K8I!z#bfuoK`v_9e4^+x^EMb5SLPWYwf->z*(WJ_z z%qR~mKZ4zL+rt!NGKgeRw0Z=LV3aC4)Tel@Yeu|#By!0Iq`nMHY5`fAl>26q{xr!@ HH-YR2?mvKF literal 0 HcmV?d00001 diff --git a/demodump/target/classes/org/example/strategy/IDEA.class b/demodump/target/classes/org/example/strategy/IDEA.class new file mode 100644 index 0000000000000000000000000000000000000000..1476e94bb3ef0780776673fe1b5d6296e3ee945f GIT binary patch literal 544 zcmZ`$%}N4M6#lM%<4lv8W@TFGPFpmapoI}cKM+U>hWo2trYqx&T*t^|?+~q9wyi}$ zL=Vu5_!iL_4J)wCIh^zToO|y1_PeKRyAZbtTDv)q$~Y> zhH$CeV2B)gZNacmm997&belq5@TNnZRMl&7r@@sB>hEU6zm$E3T-8&yC9Zk5=LoCs zEA9(>Xq_G%A5>^U%WF&5W-#jBK()k)4E*!}-R|%!ZXym{L(W7V>kNs}?L&YSMsIhc z`x^}#CN@!E$o@NoAsHB14tH(qyx9>gpCLb!UQ?c}c=wN@KJ5E~T48TMx=eNKB|WRA zr0dsxC3u(CNY7|9XuW{sxTGshgyTn$idL?a8z>@5-%mukMTj!(O3|RoqRc2qw_d;= z$M77*fD9s-6wL_%^GHxdXPJt3$7X>3gitE{jL46HK@f-_ZBx=z^jj&Y zMF)NWKa_Z0651-Y|GeCL?s?~)d-Hz%{_zvQ9-2BbsHBm^-3;#GzJ@Ig+d5LH=me|s zsA;I{NTZ^mA)pO>&+)CEz(&jS9n=0`^#)zr90tA>*v?1ubyVLMP@lSeH#iU|R~J)j zX9CIR-i0lYYq@><<*3)T{Wn&-OJup_b*%20<-0PE!DR5x9a4CqZqN4VLU-&S_9RfM z))roo{NW5g+wm^kzH=H6Cy=OyW_y;~7bsNUwl1yrmf5xXj!6=!`(fSk9ok!&Qx?d? z8>GGCzp_I%KlMg_$3Al95OKxs2l%htH&5 z&cK?~S)a=~>~!71kXG{3F<-SW?N0E&8jjvEXGgGj{Gm8Yz92N3>e@j?qR*2YG!74M}3snWxZmo z)DT<^WsHar%Lv?@7&;(Z8dCo;qlf4k?H6Z^s1qaV#0VEkbvfJJ60KS0df)BS4f^;A$2~6I)>J2e8aT}N)yrvo$L^rwkXgn@PMN| mr0gClc!VuH4%?L?Nvz`zIXS+94Q^DBlfot=g>i`$1?4}?x6)q# literal 0 HcmV?d00001 diff --git a/demodump/target/test-classes/org/example/Test.class b/demodump/target/test-classes/org/example/Test.class new file mode 100644 index 0000000000000000000000000000000000000000..dc571c291fbc305f7c16ab63280fabe4a905c966 GIT binary patch literal 1561 zcma)6*-{fh6g|y0$uK|+f(xL6AOV#T#f_*a5d;m31hFiin$U)U$#iNKO1{KDu*xcZ zkOx#A`~b_((BdyFZ%)y;anW&aY0$S zljOaW1j9fh25~utD>|-XNQ0^28biZ9%dz|i47I6&C5F0;ToDW{S<4Z#L2+5Qi+tH8 zOEN3-++O0YrS=tLoxf^%q0cRt;w>+hY+)`6&rcIlky{Q!d+K#|jc;(%=1#%P`L5*@ z(&0(mEl_Uj5pISin-`Z?c(20fC6;HRk`A|G~jLmqb>DF8@Px22{e=Gfq^kR)bPl_I5G^z z?uVVso zQ#aP<+*uhe7{*dZG%0%|%DgK);rMgzgzbqb?yT6trFKVH2~|NN-m=YPAhr#R0U!=8**Kk*-+i2NB)NAed%s8G#0Bn z4GYxVtd#3PiIh0)IKmC5B4}7YqZ6s%m6crR~u{d>pz-PzK$0KrLN&bTiPig?1*r8NKTVzkq$F zMGX@4YzU1>oS?N)4nPvEw2R{;+Q_1k=Rz=`D_QduwOgorwT=3hJCuzbXuvnbYEcDk zrf|^LaSB7|q+7WP-i3CuBB3i$RKWX)iu%O~HyPiCu7K!Url%gdt|5kDf;>Z(8k{9I d%EK^&=a?mH5AEB@bCF_t(MNx^>PHIwe*q=|oX`LO literal 0 HcmV?d00001