diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000000000000000000000000000000000000..db5f12955691506605c7b50297b402dd1489554d --- /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 0000000000000000000000000000000000000000..97626ba45445dc9f3afa66e6a149914dc39e3df6 --- /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 0000000000000000000000000000000000000000..6560a98983ec708cf9d8b5c5c3776d7bd39c475b --- /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 0000000000000000000000000000000000000000..2fc6c342ae80aa5c2231a10709cb9a4a868cb7f7 --- /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 0000000000000000000000000000000000000000..d97338e88b8cfe52a1083e7b0b67216e60a4b7d4 --- /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 0000000000000000000000000000000000000000..1f08b88792b9888be9616fef9c5b20a92251d6e1 --- /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 0000000000000000000000000000000000000000..8e2f1cf5849003dbcf548e198fe97b17ba18a333 --- /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 Binary files /dev/null and b/out/production/patterns/com/chen/InterpreterPattern/InterpreterMain.class differ diff --git a/out/production/patterns/com/chen/InterpreterPattern/po/AndExpression.class b/out/production/patterns/com/chen/InterpreterPattern/po/AndExpression.class new file mode 100644 index 0000000000000000000000000000000000000000..c03865dd615f609211e3e2da64a1e0823d5e42b5 Binary files /dev/null and b/out/production/patterns/com/chen/InterpreterPattern/po/AndExpression.class differ diff --git a/out/production/patterns/com/chen/InterpreterPattern/po/Expression.class b/out/production/patterns/com/chen/InterpreterPattern/po/Expression.class new file mode 100644 index 0000000000000000000000000000000000000000..5f26da101d8c84b1725a674cb8a1c595f1852abe Binary files /dev/null and b/out/production/patterns/com/chen/InterpreterPattern/po/Expression.class differ diff --git a/out/production/patterns/com/chen/InterpreterPattern/po/OrExpression.class b/out/production/patterns/com/chen/InterpreterPattern/po/OrExpression.class new file mode 100644 index 0000000000000000000000000000000000000000..af4a96dbf1078df893ccb6809f6c57b7f9aa938b Binary files /dev/null and b/out/production/patterns/com/chen/InterpreterPattern/po/OrExpression.class differ diff --git a/out/production/patterns/com/chen/InterpreterPattern/po/TerminalExpression.class b/out/production/patterns/com/chen/InterpreterPattern/po/TerminalExpression.class new file mode 100644 index 0000000000000000000000000000000000000000..3c45ea7351ab7444aff626999a8c736f18a950ea Binary files /dev/null and b/out/production/patterns/com/chen/InterpreterPattern/po/TerminalExpression.class differ 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 Binary files /dev/null and b/out/production/untitled/com/company/InterpreterPattern/AndExpression.class differ diff --git a/out/production/untitled/com/company/InterpreterPattern/Expression.class b/out/production/untitled/com/company/InterpreterPattern/Expression.class new file mode 100644 index 0000000000000000000000000000000000000000..0bdff511ad3927dccf05de0c9b9b485f7ca200ec Binary files /dev/null and b/out/production/untitled/com/company/InterpreterPattern/Expression.class differ 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 Binary files /dev/null and b/out/production/untitled/com/company/InterpreterPattern/InterpreterMain.class differ 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 Binary files /dev/null and b/out/production/untitled/com/company/InterpreterPattern/OrExpression.class differ 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 0000000000000000000000000000000000000000..1de125d5328d5d1b8763d9b36f8ef431910b0a4b Binary files /dev/null and b/out/production/untitled/com/company/InterpreterPattern/TerminalExpression.class differ diff --git a/out/production/untitled/com/company/Main.class b/out/production/untitled/com/company/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..d75b0474317baa8b5addbf94ca11078a589ea32d Binary files /dev/null and b/out/production/untitled/com/company/Main.class differ diff --git a/patterns.iml b/patterns.iml new file mode 100644 index 0000000000000000000000000000000000000000..c90834f2d607afe55e6104d8aa2cdfffb713f688 --- /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 0000000000000000000000000000000000000000..97d933dd48316c0cea24201171387711df40a5aa --- /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 0000000000000000000000000000000000000000..38ba8b12003ac9255637a15a8c7c1382a9fe59c1 --- /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 0000000000000000000000000000000000000000..31cad07178ff91507eee1cc69ae464de97bc676a --- /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 0000000000000000000000000000000000000000..c5675f5409806a6838f3d487e15f556e4e70afbc --- /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 0000000000000000000000000000000000000000..fc25670420d70c296e707b2b23861639957ab99a --- /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 0000000000000000000000000000000000000000..072b8e4fc62ff0b3b4a5659cb2710a63fae435c9 --- /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 0000000000000000000000000000000000000000..fd0529f6b8be00d75a65387e442d4822b8b2a732 --- /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 0000000000000000000000000000000000000000..6a110fb07f8e7b8cfca076fc0119108cdd7a7ffc --- /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 0000000000000000000000000000000000000000..ed0fef5ed4735b438b65e287daecadb558ac196a --- /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 0000000000000000000000000000000000000000..cf77b9736c3dc2abf1cb00caed5ac8beb25ca012 --- /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 0000000000000000000000000000000000000000..107cec0d176b5a43ed8e28206c8c516e4f17cc8f --- /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 0000000000000000000000000000000000000000..41f46f8d275d6f9bfca66f6183724bc8785b2719 --- /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 0000000000000000000000000000000000000000..ad8fac9972418218a73b1536990e120abdd8f01e --- /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); +}