HeadFir st 设计模式学习笔记21-- 解释者(Inter pr eter)模式拾零

HeadFir st 设计模式学习笔记21-- 解释者(Inter pr eter)模式拾零
1.概述
这个模式是在不能叫做模式,它的作用是实现⼀种语言规范的解释器,比如罗马数字解释器。
2.实例
我们在这创建⼀个决策器。通过构建有两种语言,OR和AND,可以想象为⼀个单位,领导给出
⼀些决策意见,比如谁或者谁提拔,谁和谁提拔等(这里面有嵌套),然后存入这个决策器中
。主程序通过输入几个人名的组合得出⼀个是不是可能这些人被提拔的决策。
首先,我们对决策器中的语言进行⼀个抽象:
1: public abstract class Expression {
2:     abstract public boolean interpret(String str);
3: }
里面提供了⼀个对决策器中语句进行解释的接口。
然后,我们对参与提拔的输入进行构建:
1: public class TerminalExpression extends Expression {
2:
3:     private String literal = null;
4:
5:     public TerminalExpression(String str) {
6:         literal = str;
7:     }
8: 
9:     @Override
10:     public boolean interpret(String str) {
11:         StringTokenizer st = new StringTokenizer(str);
12:         while (st.hasMoreTokens()) {
13:             String test = st.nextToken();
14:             if (test.equals(literal)) {
15:                 return true;
16:             }
17:         }
18:         return false;
19:     }
20:
21: }
随后对两个表达式进行解释
1: public class OrExpression extends Expression {
2:
3:     private Expression expression1 = null;
4:     private Expression expression2 = null;
5:
6:     public OrExpression(Expression expression1, Expression expression2) {
7:         this.expression1 = expression1;
8:         this.expression2 = expression2;
9:     }
10:
11:     public boolean interpret(String str) {
12:         return expression1.interpret(str) || expression2.interpret(str);
13:     }
14:
15: }
1: public class AndExpression extends Expression {
2:     private Expression expression1 = null;
3:     private Expression expression2 = null;
4:
5:     public AndExpression(Expression expression1, Expression expression2) {
6:         this.expression1 = expression1;
7:         this.expression2 = expression2;
8:     }
9:
10:     public boolean interpret(String str) {
11:         return expression1.interpret(str) && expression2.interpret(str);
12:     }
13: }
现在我们就可以构建⼀个决策,然后试⼀试这个决策器的作用了:
1: public class MainInterpreter {
2:
3:     /**
4:      * this method builds the interpreter tree
5:      * It defines the rule "Owen and (John or (Henry or Mary))"
6:      * @return
7:      */
8:     static Expression buildInterpreterTree()
9:     {
10:         // Literal
11:         Expression terminal1 = new TerminalExpression("John");
12:         Expression terminal2 = new TerminalExpression("Henry");
13:         Expression terminal3 = new TerminalExpression("Mary");
14:         Expression terminal4 = new TerminalExpression("Owen");
15:
16:         // Henry or Mary
17:         Expression alternation1 = new OrExpression(terminal2, terminal3);
18:
19:         // John or (Henry or Mary)
20:         Expression alternation2 = new OrExpression(terminal1, alternation1);
21:
22:         // Owen and (John or (Henry or Mary))
23:         return new AndExpression(terminal4, alternation2);
24:     }
25:
26:  
27:     /**
28:      * main method - build the interpreter
29:      *  and then interpret a specific sequence
30:      * @param args
31:      */
32:     public static void main(String[] args) {
33:      
34:         String context = "John";
35:
36:         Expression define = buildInterpreterTree();
37:
38:         System.out.println(context + " is " + define.interpret(context));
39:     }
40: }

你可能感兴趣的:(st,HeadFir,设计模式学习笔记2)