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..a461ab0ba6cbe8437d1850d3a0a1c3de48cd760b 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..23db929e95eae0ef0c2032a9b00db3f6a0eccdcb 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/patterns/com/chen/Main.class b/out/production/patterns/com/chen/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..7e9403a55142821afe45e18faf03366875057e5b Binary files /dev/null and b/out/production/patterns/com/chen/Main.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..d625f2ad1a6666541b229adb593ea5ac207ebc9e --- /dev/null +++ b/src/com/chen/InterpreterPattern/package-info.java @@ -0,0 +1,4 @@ +/** + * 解释器模式 + */ +package com.chen.InterpreterPattern; 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/PatternMain.java b/src/com/chen/PatternMain.java new file mode 100644 index 0000000000000000000000000000000000000000..9b948a5e8cb8ee98b1c9ac0edab457fd7c5cf989 --- /dev/null +++ b/src/com/chen/PatternMain.java @@ -0,0 +1,19 @@ +package com.chen; + +import com.chen.InterpreterPattern.InterpreterMain; + +/** + * @author lx + */ +public class PatternMain { + + public static void main(String[] args) { + + //解释器模式 + new InterpreterMain().main(); + + + + } + +}