From 02107b8d2232e61f27e2398102e288d913a77360 Mon Sep 17 00:00:00 2001 From: chenjianqiang Date: Wed, 13 Oct 2021 10:51:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AD=96=E7=95=A5=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E6=A1=88=E4=BE=8B=20Signed-off-by:=20chenjianqiang=20?= =?UTF-8?q??= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 0 .idea/description.html | 1 + .idea/encodings.xml | 6 + .idea/inspectionProfiles/Project_Default.xml | 36 +++++ .idea/misc.xml | 12 ++ .idea/modules.xml | 8 ++ .idea/project-template.xml | 3 + .idea/workspace.xml | 125 ++++++++++++++++++ .../InterpreterPattern/InterpreterMain.class | Bin 0 -> 2498 bytes .../InterpreterPattern/po/AndExpression.class | Bin 0 -> 803 bytes .../InterpreterPattern/po/Expression.class | Bin 0 -> 176 bytes .../InterpreterPattern/po/OrExpression.class | Bin 0 -> 801 bytes .../po/TerminalExpression.class | Bin 0 -> 1716 bytes .../InterpreterPattern/AndExpression.class | Bin 0 -> 901 bytes .../InterpreterPattern/Expression.class | Bin 0 -> 176 bytes .../InterpreterPattern/InterpreterMain.class | Bin 0 -> 2558 bytes .../InterpreterPattern/OrExpression.class | Bin 0 -> 1086 bytes .../TerminalExpression.class | Bin 0 -> 1716 bytes .../untitled/com/company/Main.class | Bin 0 -> 460 bytes patterns.iml | 11 ++ .../InterpreterPattern/InterpreterMain.java | 55 ++++++++ .../chen/InterpreterPattern/package-info.java | 9 ++ .../InterpreterPattern/po/AndExpression.java | 28 ++++ .../InterpreterPattern/po/Expression.java | 18 +++ .../InterpreterPattern/po/OrExpression.java | 28 ++++ .../po/TerminalExpression.java | 46 +++++++ src/com/chen/PatternApplication.java | 21 +++ src/com/chen/StrategyPattern/Context.java | 22 +++ .../chen/StrategyPattern/StrategyMain.java | 25 ++++ .../chen/StrategyPattern/package-info.java | 7 + .../chen/StrategyPattern/po/OperationA.java | 19 +++ .../chen/StrategyPattern/po/OperationB.java | 19 +++ src/com/chen/StrategyPattern/po/Strategy.java | 14 ++ 33 files changed, 513 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/description.html create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/project-template.xml create mode 100644 .idea/workspace.xml create mode 100644 out/production/patterns/com/chen/InterpreterPattern/InterpreterMain.class create mode 100644 out/production/patterns/com/chen/InterpreterPattern/po/AndExpression.class create mode 100644 out/production/patterns/com/chen/InterpreterPattern/po/Expression.class create mode 100644 out/production/patterns/com/chen/InterpreterPattern/po/OrExpression.class create mode 100644 out/production/patterns/com/chen/InterpreterPattern/po/TerminalExpression.class create mode 100644 out/production/untitled/com/company/InterpreterPattern/AndExpression.class create mode 100644 out/production/untitled/com/company/InterpreterPattern/Expression.class create mode 100644 out/production/untitled/com/company/InterpreterPattern/InterpreterMain.class create mode 100644 out/production/untitled/com/company/InterpreterPattern/OrExpression.class create mode 100644 out/production/untitled/com/company/InterpreterPattern/TerminalExpression.class create mode 100644 out/production/untitled/com/company/Main.class create mode 100644 patterns.iml create mode 100644 src/com/chen/InterpreterPattern/InterpreterMain.java create mode 100644 src/com/chen/InterpreterPattern/package-info.java create mode 100644 src/com/chen/InterpreterPattern/po/AndExpression.java create mode 100644 src/com/chen/InterpreterPattern/po/Expression.java create mode 100644 src/com/chen/InterpreterPattern/po/OrExpression.java create mode 100644 src/com/chen/InterpreterPattern/po/TerminalExpression.java create mode 100644 src/com/chen/PatternApplication.java create mode 100644 src/com/chen/StrategyPattern/Context.java create mode 100644 src/com/chen/StrategyPattern/StrategyMain.java create mode 100644 src/com/chen/StrategyPattern/package-info.java create mode 100644 src/com/chen/StrategyPattern/po/OperationA.java create mode 100644 src/com/chen/StrategyPattern/po/OperationB.java create mode 100644 src/com/chen/StrategyPattern/po/Strategy.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6560a98 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2fc6c34 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d97338e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..8e2f1cf --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1634023392076 + + + + + + \ No newline at end of file diff --git a/out/production/patterns/com/chen/InterpreterPattern/InterpreterMain.class b/out/production/patterns/com/chen/InterpreterPattern/InterpreterMain.class new file mode 100644 index 0000000000000000000000000000000000000000..9a916e2cf581ef9e1ed1b426288df67ecc403716 GIT binary patch literal 2498 zcmb7G+jkRX5dUq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w{Stvg1At;zleOlO9t+*>STX&FVM7Ov0LYpyHrs@}_b$*eO# z`V{wCVT;+;IDHRgMt&sQ)Ax0Yj%6`D#hea)t4nw5o>66Br$O*U3&(Wwt7N^$tPELI zWb+F>ttMY;%E+?+LAB$YScp#^Y1%B{{7E)0?n<|8e9|!Nic!oeHg$+V68sAw?Ls~= z1umaNYhTf9&l?ezwVGjX3rzNM>AD7OzOd#Cnay@vyXnN^i02#+Z6woeNQ<8LZyhPV zZ6n|@|8YfdejnpQ*oP$lg1kRZd4ZZ$1gQZ=#t$LJkI=V~`yPQq1iwS*Yv1Du$^ir@ zhiDro@-Uv{ZnWixr!dAH5vWNKRp4n{Y@v%R{h~bE`v&3hZxL!ji$|7o6HP=9FrYNK z9}w%qeWg0H5-0>GXG6)*gF-k^&=OiA{3TM!P(quTjVR+}#JBzo;b0+nPZM7~`h~zp z&^HdzA0KF9FcECxY!gFG#1p~$j2wWNiO~0l+Z9G1oQeGAvP>e<32q57ow!5ssI20 literal 0 HcmV?d00001 diff --git a/out/production/untitled/com/company/InterpreterPattern/AndExpression.class b/out/production/untitled/com/company/InterpreterPattern/AndExpression.class new file mode 100644 index 0000000000000000000000000000000000000000..6096ffa1d7025af6f71bcbfe5963a78b31d12f3e GIT binary patch literal 901 zcmcIi%Wl&^6g}5=YLX^Rnx^Gd9sw4K)70XzD?%Uy5>j|bL0PbyxK^2xjw8oKWzSFW z1#H+L5g}Oc0elqVjAT42^Zm!yZvbxLS_wYR7qNs*3l}V0T*fI} z@^RTm)5jHo6JHl}dQox^@*6AjCVXVFI31@k@tMkqCVX&2KAGsm?AVqY7H$fbZfm2n zJA${Ze^B^8rR{;hyPxdFf@)8j_{rhHFixMV;fO2sUJ|L%K&9I9<3%rfsV9Q%MS1R- z-MRA@_3VNK>#g3tdac5dGJ9b^OSReCX}=KoQNp65%v%3dAgJ^+6}@__#>Y`g{p2u> z;)gm9J7>6U+smlnY8ge8%2>8gw$Q-3U~9pzn-de%PSQLb?#B@mwihUz9A4nI+5 zdIMM^7Z3syLT+qe-+B&&#qv?5cMY u3hk~kqE+@O_y0QfClz9~l|z6q9|n$>nsvwsgd~0fC?$-L_6p<339=tl2`{Yx literal 0 HcmV?d00001 diff --git a/out/production/untitled/com/company/InterpreterPattern/InterpreterMain.class b/out/production/untitled/com/company/InterpreterPattern/InterpreterMain.class new file mode 100644 index 0000000000000000000000000000000000000000..dbe68156ec9d66a917fd4d853ac15a942d2f9f19 GIT binary patch literal 2558 zcmcIm+jA3T6#s3Kb~ovkLQ0E8+Hw(W(@MCBNC8EpiWXW-1p}gP)1_TVHg&Uw)*F?! z+@y@m=s4mO2Sjm3839{TS}sq%=zrm4)AGctPv|+DCQWIbJkidc^PThk&dKk5`<;{g z`DpYWfOR%Xxcz?qo<%r@BD|(whU>cgv5{Xw6^> zjz*2c8OyDN!;MBN+H;VS?24LNh*EiH%@pfU_0}{*AXrU9C>rt8)1m0y{vEoh8Xbm8 zvPm&X>i+FrWXED!R3|2$%%?RASeq-fSv4Y>u7szhkh2P=Byv`5Y-SZlraRObGoumJ zhzZEwr0JTuMS!!adXE6twrDHyFKWRJD+#1_sUZO?s%9Q~^-LFCPdds#w9^Qw z^%`@Y|JdiQd28Sj zt;et2OWYr%wG2OR%Huzuv(8+yu6%FZ9%5|#=7XM|#GUgpR`Gtmb?s~Gr;`M<-2>K% z?-HYzUtRx`?*92)eiJ^jc@XNsRuQolV9jE_QW}?wEPS`K^5-;{|Mx z@gj?jEb6d9#HTVo!!ZF1w#@z~<8|zi(Ssc#=uD5Jm-;gYfktDhy9CtcK-1Wmf-}=T z7X31w$9fSbWSqn)0V{K4dF--IyP}OPht!ZMptNe1QO13neVw~wrW&D!j&@Q+Nvct_ zsK1Hko=MYBRU*`Y`Jt#DQV39NH^TIN0b&QE!#X9Ht^4Q``>Jwv{nVM%JsBt$9(+PH zICpi9H#SuuwLKkIYRLc3k?q40`kJbx?^Br`8Y?8h6U5duDQr!%z}D3Hwx%|=HQgjz zQv=wVdcfAy2DYY7ur;+8X(t4pawK0O>ks773OMGAL-6I@g(K+nIs-wM*A*B>UfPq- z9+5m!+T&)Ahdc$o{8W_8p29#d&pU_QMLzG`Va)S+y~8N>Nh!#DhLkX*G!V@9E(iog z?^6UWyFnBjc$wr|Nq3bH%ar7M9(@Gks3s>c4+8cV`y&aulJ0{wwK4E~c5b6irwLg>)PJAYtyMRBIE} zvYlR_iaxQGq7*?<@r>BCR$~c~l zqiPt{WK`GOhR=Zu@Q_h_6(Xs5*C{;3m47h-)x@eM!--N)GT{}o6TqBAIM#0y@dlZ1 IlIcLhe}dchKmY&$ literal 0 HcmV?d00001 diff --git a/out/production/untitled/com/company/InterpreterPattern/OrExpression.class b/out/production/untitled/com/company/InterpreterPattern/OrExpression.class new file mode 100644 index 0000000000000000000000000000000000000000..b169cff01565dd8fcd2c0f8739a17b30d5b27f1c GIT binary patch literal 1086 zcmb7DO-~b16g_XIGVL(^V6dp5h+o(bU@W38Fvi4aVp4^qrU~w*;{*rWnPxgQuYC~^WOJgU%mpEzy%rI7z{zi zQ67duIELdvoRD!6r-B%kaT!M{_-X(^a#2$+GkebXh1g-Qo(d@>4hS# zs9DF*i+SC(IBqQlyt`I~VYHLTl)Ga)A?we68RCiToPJ-|in=|kWCw!n(J_b#Zc-abZlU6tCHmo1i7MV4mE~yDG)Rxe zY3e09KkWtni0L%Zd<~syhIcbyw~_byt?%R}pUPaRX#dB_JlzO>Nf=!M0V;QZZjQ=L zptnHlG4xPW3C$1|=^agd21})vAf!f?Ab#)@dnu|2Pz+OyAmmfTu%A|jnq`Qij}Q^~ zGR*(tLR^NtYROb7TcCfLj|iyy(#z;lB?`+3ranPPuOK9#ju-*437$dq#_Z# l5!ol`r_Klb2Kt(We;df)5c)}OfO5NtM!d`j9VR5;$Zy%~7R>+v literal 0 HcmV?d00001 diff --git a/out/production/untitled/com/company/InterpreterPattern/TerminalExpression.class b/out/production/untitled/com/company/InterpreterPattern/TerminalExpression.class new file mode 100644 index 0000000000000000000000000000000000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al 0 HcmV?d00001 diff --git a/patterns.iml b/patterns.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/patterns.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/com/chen/InterpreterPattern/InterpreterMain.java b/src/com/chen/InterpreterPattern/InterpreterMain.java new file mode 100644 index 0000000..97d933d --- /dev/null +++ b/src/com/chen/InterpreterPattern/InterpreterMain.java @@ -0,0 +1,55 @@ +package com.chen.InterpreterPattern; + +import com.chen.InterpreterPattern.po.AndExpression; +import com.chen.InterpreterPattern.po.Expression; +import com.chen.InterpreterPattern.po.OrExpression; +import com.chen.InterpreterPattern.po.TerminalExpression; + +/** + * 解释器模式触发器 + * + * @author lx + * @date 2021/10/13 9:23 + **/ +public class InterpreterMain { + String[] cityAndFree = new String[]{"杭州", "攀枝花"}; + String[] cardAndFree = new String[]{"市民卡", "社保卡"}; + String[] workAndFree = new String[]{"税务够年", "已退休"}; + + String[] allOrFree = new String[]{"孕妇", "退伍军人", "儿童"}; + + String freeNotice = "滴,免费卡!"; + String spendNotice = "滴,扣费一万元!余额不足,请下去!"; + + String[] customers = new String[]{"已退休的杭州的市民卡", "税务够年的上海的市民卡", "杭州的社保卡的已退休", "成都的退伍军人", "北京的市民卡"}; + + Expression e; + TerminalExpression allOrFreeT; + TerminalExpression cityAndFreeT; + TerminalExpression cardAndFreeT; + TerminalExpression workAndFreeT; + + public void main() { + + allOrFreeT = new TerminalExpression(allOrFree); + cityAndFreeT = new TerminalExpression(cityAndFree); + cardAndFreeT = new TerminalExpression(cardAndFree); + workAndFreeT = new TerminalExpression(workAndFree); + + for (String customer : customers) { + + System.out.println("欢迎“".concat(customer).concat("”乘车,正在检查你的卡:")); + + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>".concat(exec(customer))); + + } + + } + + private String exec(String customer) { + + e = new OrExpression(allOrFreeT, new AndExpression(cityAndFreeT, new AndExpression(cardAndFreeT, workAndFreeT))); + + return !e.interpret(customer) ? spendNotice : freeNotice; + } +} diff --git a/src/com/chen/InterpreterPattern/package-info.java b/src/com/chen/InterpreterPattern/package-info.java new file mode 100644 index 0000000..38ba8b1 --- /dev/null +++ b/src/com/chen/InterpreterPattern/package-info.java @@ -0,0 +1,9 @@ +/** + * 解释器模式 + */ +package com.chen.InterpreterPattern; +/* + * 使用场景:执行多个固定的终端计算,将多个结果进行与或的判断 + * 类说明细:Expression:接口起点;OrExpression:将两个子计算的结果进行或判断;AndExpression:将两个子计算的结果进行与判断; + * 注意事项:一个子计算的结果可以是孙子计算的结果 + */ diff --git a/src/com/chen/InterpreterPattern/po/AndExpression.java b/src/com/chen/InterpreterPattern/po/AndExpression.java new file mode 100644 index 0000000..31cad07 --- /dev/null +++ b/src/com/chen/InterpreterPattern/po/AndExpression.java @@ -0,0 +1,28 @@ +package com.chen.InterpreterPattern.po; + +/** + * 与 + * @author lx + * @date 2021/10/13 9:33 + **/ +public class AndExpression implements Expression{ + + private final Expression e1; + private final Expression e2; + + public AndExpression(Expression e1, Expression e2) { + this.e1 = e1; + this.e2 = e2; + } + + /** + * 解释 + * + * @param context 正文 + * @return true、false + */ + @Override + public boolean interpret(String context) { + return e1.interpret(context) && e2.interpret(context); + } +} diff --git a/src/com/chen/InterpreterPattern/po/Expression.java b/src/com/chen/InterpreterPattern/po/Expression.java new file mode 100644 index 0000000..c5675f5 --- /dev/null +++ b/src/com/chen/InterpreterPattern/po/Expression.java @@ -0,0 +1,18 @@ +package com.chen.InterpreterPattern.po; + +/** + * 表达 + * + * @author lx + * @date 2021/10/13 9:20 + **/ +public interface Expression { + + /** + * 解释 + * + * @param context 正文 + * @return true、false + */ + boolean interpret(String context); +} diff --git a/src/com/chen/InterpreterPattern/po/OrExpression.java b/src/com/chen/InterpreterPattern/po/OrExpression.java new file mode 100644 index 0000000..fc25670 --- /dev/null +++ b/src/com/chen/InterpreterPattern/po/OrExpression.java @@ -0,0 +1,28 @@ +package com.chen.InterpreterPattern.po; + +/** + * 或 + * @author lx + * @date 2021/10/13 9:33 + **/ +public class OrExpression implements Expression{ + + private final Expression e1; + private final Expression e2; + + public OrExpression(Expression e1, Expression e2) { + this.e1 = e1; + this.e2 = e2; + } + + /** + * 解释 + * + * @param context 正文 + * @return true、false + */ + @Override + public boolean interpret(String context) { + return e1.interpret(context) || e2.interpret(context); + } +} diff --git a/src/com/chen/InterpreterPattern/po/TerminalExpression.java b/src/com/chen/InterpreterPattern/po/TerminalExpression.java new file mode 100644 index 0000000..072b8e4 --- /dev/null +++ b/src/com/chen/InterpreterPattern/po/TerminalExpression.java @@ -0,0 +1,46 @@ +package com.chen.InterpreterPattern.po; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * 终端计算 + * + * @author lx + * @date 2021/10/13 9:33 + **/ +public class TerminalExpression implements Expression { + + private final Set data; + + public TerminalExpression(String[] data) { + this.data = new HashSet<>(Arrays.asList(data)); + } + + /** + * 解释 + * + * @param context 被验证元素 + * @return true、false + */ + @Override + public boolean interpret(String context) { + + System.out.println(data); + + String splitFux = "的"; + if(!context.contains(splitFux)){ + return data.contains(context); + } + String[] contexts = context.split(splitFux); + boolean result = false; + for (String s : contexts) { + if (data.contains(s)) { + result = true; + } + } + System.out.println("结果:" + result); + return result; + } +} diff --git a/src/com/chen/PatternApplication.java b/src/com/chen/PatternApplication.java new file mode 100644 index 0000000..fd0529f --- /dev/null +++ b/src/com/chen/PatternApplication.java @@ -0,0 +1,21 @@ +package com.chen; + +import com.chen.InterpreterPattern.InterpreterMain; +import com.chen.StrategyPattern.StrategyMain; + +/** + * @author lx + */ +public class PatternApplication { + + public static void main(String[] args) { + + //解释器模式 +// new InterpreterMain().main(); + + //策略模式 + new StrategyMain().main(); + + } + +} diff --git a/src/com/chen/StrategyPattern/Context.java b/src/com/chen/StrategyPattern/Context.java new file mode 100644 index 0000000..6a110fb --- /dev/null +++ b/src/com/chen/StrategyPattern/Context.java @@ -0,0 +1,22 @@ +package com.chen.StrategyPattern; + +import com.chen.StrategyPattern.po.Strategy; + +/** + * @author lx + * @date 2021/10/13 11:30 + **/ +public class Context { + /** + * 本次策略规则对象 + */ + private final Strategy strategy; + + public Context(Strategy strategy) { + this.strategy = strategy; + } + + public int exec(int a, int b) { + return strategy.exec(a, b); + } +} diff --git a/src/com/chen/StrategyPattern/StrategyMain.java b/src/com/chen/StrategyPattern/StrategyMain.java new file mode 100644 index 0000000..ed0fef5 --- /dev/null +++ b/src/com/chen/StrategyPattern/StrategyMain.java @@ -0,0 +1,25 @@ +package com.chen.StrategyPattern; + +import com.chen.StrategyPattern.po.OperationA; +import com.chen.StrategyPattern.po.OperationB; + +/** + * @author lx + * @date 2021/10/13 11:32 + **/ +public class StrategyMain { + + public void main() { + int a = 5; + int b = 11; + + + Context c = new Context(new OperationA()); + System.err.println(c.exec(a, b)); + + + c = new Context(new OperationB()); + System.err.println(c.exec(a, b)); + } + +} diff --git a/src/com/chen/StrategyPattern/package-info.java b/src/com/chen/StrategyPattern/package-info.java new file mode 100644 index 0000000..cf77b97 --- /dev/null +++ b/src/com/chen/StrategyPattern/package-info.java @@ -0,0 +1,7 @@ +/** + * 策略模式 + */ +package com.chen.StrategyPattern; +/* + * 传入不同的计算策略,对同一对值执行对应的计算逻辑 + */ diff --git a/src/com/chen/StrategyPattern/po/OperationA.java b/src/com/chen/StrategyPattern/po/OperationA.java new file mode 100644 index 0000000..107cec0 --- /dev/null +++ b/src/com/chen/StrategyPattern/po/OperationA.java @@ -0,0 +1,19 @@ +package com.chen.StrategyPattern.po; + +/** + * @author lx + * @date 2021/10/13 11:28 + **/ +public class OperationA implements Strategy{ + /** + * 策略执行方法 + * + * @param a 值 + * @param b 值 + * @return 计算结果值 + */ + @Override + public int exec(int a, int b) { + return a + b; + } +} diff --git a/src/com/chen/StrategyPattern/po/OperationB.java b/src/com/chen/StrategyPattern/po/OperationB.java new file mode 100644 index 0000000..41f46f8 --- /dev/null +++ b/src/com/chen/StrategyPattern/po/OperationB.java @@ -0,0 +1,19 @@ +package com.chen.StrategyPattern.po; + +/** + * @author lx + * @date 2021/10/13 11:28 + **/ +public class OperationB implements Strategy{ + /** + * 策略执行方法 + * + * @param a 值 + * @param b 值 + * @return 计算结果值 + */ + @Override + public int exec(int a, int b) { + return a * b; + } +} diff --git a/src/com/chen/StrategyPattern/po/Strategy.java b/src/com/chen/StrategyPattern/po/Strategy.java new file mode 100644 index 0000000..ad8fac9 --- /dev/null +++ b/src/com/chen/StrategyPattern/po/Strategy.java @@ -0,0 +1,14 @@ +package com.chen.StrategyPattern.po; + +/** + * @author lx + */ +public interface Strategy { + /** + * 策略执行方法 + * @param a 值 + * @param b 值 + * @return 计算结果值 + */ + int exec(int a, int b); +} -- GitLab