zoukankan      html  css  js  c++  java
  • 【HeadFirst 设计模式学习笔记】 20 解释者(Interpreter)模式拾零

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

    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: }

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

  • 相关阅读:
    再谈ORACLE CPROCD进程
    fopen()函数
    Java抓取网页数据(原网页+Javascript返回数据)
    Vmware ESX 5.0 安装与部署
    PostgreSQL服务端监听设置及client连接方法
    方向梯度直方图(HOG)和颜色直方图的一些比較
    Vim简明教程【CoolShell】
    FileSystemWatcher使用方法具体解释
    几种常见模式识别算法整理和总结
    ThreadPool.QueueUserWorkItem的性能问题
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2827666.html
Copyright © 2011-2022 走看看