设计模式初探-解释器模式

来源:互联网 发布:淘宝排名突然下降 编辑:程序博客网 时间:2024/06/05 05:45

解释器模式(INTERPRETER),通过面向对象的方式构造语言解释器,并使用该解释器按照一定的文法解释语言中的句子,属于类行为模式。记得大学时候参加过机器人足球比赛,通过面板输入指令"up move 10 and left run 8",足球机器人就会执行相应的动作。将这些指令组合就能完成高难度的射门,躲避,可谓智能也!下面将通过机器人指令解释来阐述解释器模式的强大。

一、使用场景

1、当有一个语言需要解释执行,并能从该语言的句子中抽象出语法树时,比如机器人足球比赛的指令,也就up,down,move等有限的基础操作码。

2、抽象出的文法要简单,不至于导致文法的类层次过于庞大而无法管理。对于复杂的文法可以使用语法分析程序生成器,这样无需构造抽象语法树,节省时间和空间。

3、效率不是关键的情况。高效的解释器通常不采用直接解释抽象语法树实现,而是通过转换其他形式实现,比如状态机。

二、UML图

解释器uml图

三、Java实现

[java] view plain copy
  1. package study.patterns.interpreter;  
  2.   
  3. import java.util.Stack;  
  4. /** 
  5.  * 解释器模式,用面向对象的方法构造简单的语言解释器。 
  6.  * 示例:使用解释器模式实现机器人指令的解析。 
  7.  * 指令文法: 
  8.  * expression ::= direction action distance | composite //表达式 
  9.  * composite ::= expression 'and' expression //复合表达式 
  10.  * direction ::= 'up' | 'down' | 'left' | 'right' //移动方向 
  11.  * action ::= 'move' | 'run' //移动方式 
  12.  * distance ::= an integer //移动距离 
  13.  * 终结符表达式:direction、action和distance,语言的最小组成单位,不能再拆分。 
  14.  * 非终结符表达式:expression和composite,完整的句子,包含一系列终结符或非终结符。 
  15.  * 抽象语法树:将指令按照语法抽象为树形结构(可以使用栈模拟),是简单句子解释的重要步骤。 
  16.  * @author qbg 
  17.  */  
  18. public class InterpreterPattern {  
  19.     public static void main(String[] args) {  
  20.         String sentence = "up move 8 and right run 10 and left move 6";//输入指令要严格按照文法  
  21.         InstructionHandler handler = new InstructionHandler();  
  22.         handler.handle(sentence);//抽象语法树  
  23.         System.out.println(handler.interpret());//解释输入指令并输出  
  24.     }  
  25. }  
  26. /** 
  27.  * 抽象表达式,可以采用接口或抽象类实现  
  28.  */  
  29. interface INode{  
  30.     public String interpret();  
  31. }  
  32. /** 
  33.  * And解释,用于将两个表达式进行and操作,非终止表达式  
  34.  */  
  35. class AndNode implements INode{  
  36.     private INode left;//and的左表达式  
  37.     private INode right;//and的右表达式  
  38.       
  39.     public AndNode(INode left,INode right){  
  40.         this.left = left;  
  41.         this.right = right;  
  42.     }  
  43.       
  44.     /** 
  45.      * And表达式解释操作,两个操作的联合 
  46.      */  
  47.     @Override  
  48.     public String interpret() {  
  49.         return left.interpret()+"再"+right.interpret();  
  50.     }  
  51. }  
  52. /** 
  53.  * 简单的句子解释,由基本的原子解释组成,句子解释可以组合成复杂的句子  
  54.  */  
  55. class SentenceNode implements INode{  
  56.     private INode direction;//方向解释  
  57.     private INode action;//动作解释  
  58.     private INode distance;//距离解释   
  59.       
  60.     public SentenceNode(INode direction,INode action,INode distance){  
  61.         this.direction = direction;  
  62.         this.action = action;  
  63.         this.distance = distance;  
  64.     }  
  65.       
  66.     /** 
  67.      * 简单句子解释逻辑,将原子解释按规则解释就行(必须按文法规则,否则解释出错) 
  68.      */  
  69.     @Override  
  70.     public String interpret() {  
  71.         return direction.interpret()+action.interpret()+distance.interpret();  
  72.     }  
  73. }  
  74. /** 
  75.  * 方向解释,解释方向操作码(up,down,left,right)并执行相应操作,终结符表达式。 
  76.  */  
  77. class DirectionNode implements INode{  
  78.     private String direction;   
  79.       
  80.     public DirectionNode(String direction){  
  81.         this.direction = direction;  
  82.     }  
  83.       
  84.     /** 
  85.      * 方向表达式的解释操作 
  86.      */  
  87.     @Override  
  88.     public String interpret() {  
  89.         if(direction.equalsIgnoreCase("up")){  
  90.             return "向上";  
  91.         }else if(direction.equalsIgnoreCase("down")){  
  92.             return "向下";  
  93.         }else if(direction.equalsIgnoreCase("left")){  
  94.             return "向左";  
  95.         }else if(direction.equalsIgnoreCase("right")){  
  96.             return "向右";  
  97.         }else{  
  98.             return "无效操作码";  
  99.         }  
  100.     }  
  101. }  
  102.   
  103. /** 
  104.  * 动作解释,解释操作码(move,run)并执行相应操作,终结符表达式 
  105.  */  
  106. class ActionNode implements INode{  
  107.     private String action;  
  108.       
  109.     public ActionNode(String action){  
  110.         this.action = action;  
  111.     }  
  112.       
  113.     /** 
  114.      * 动作表达式的解释操作 
  115.      */  
  116.     @Override  
  117.     public String interpret() {  
  118.          if(action.equalsIgnoreCase("move")){  
  119.              return "移动";  
  120.          }else if(action.equalsIgnoreCase("run")){  
  121.              return "快速移动";  
  122.          }else{  
  123.              return "无效操作码";  
  124.          }  
  125.     }  
  126. }  
  127.   
  128. /** 
  129.  * 距离解释,用于解释移动的距离,数字,终结符表达式。  
  130.  */  
  131. class DistanceNode implements INode{  
  132.     private String distance;  
  133.       
  134.     public DistanceNode(String distance){  
  135.         this.distance = distance;  
  136.     }  
  137.       
  138.     @Override  
  139.     public String interpret() {  
  140.         return distance;  
  141.     }  
  142. }  
  143.   
  144. /** 
  145.  * 指令处理类,用于抽象语法树,工具类. 
  146.  */  
  147. class InstructionHandler{  
  148.     private INode node;  
  149.       
  150.     public void handle(String sentence){  
  151.         INode left=null,right=null,direction=null,action=null,distance=null;  
  152.         Stack<INode> stack = new Stack<INode>(); //用stack存储抽象语法树  
  153.         String[] words = sentence.split(" ");//以空格分隔操作码字符串  
  154.         for(int i=0; i<words.length; i++){  
  155.             //1、如果遇到“and”,则将后续的三个单词作为终结符练成一个简单的句子,并将其作为“and”的右表达式。  
  156.             //2、将stack栈顶弹出的表达式作为“and”的左表达式,最后将新的“and”表达式压入stack。  
  157.             if(words[i].equalsIgnoreCase("and")){  
  158.                 left = stack.pop();//将从stack弹出的作为左表达式  
  159.                 String word1 = words[++i];  
  160.                 direction = new DirectionNode(word1);  
  161.                 String word2 = words[++i];  
  162.                 action = new ActionNode(word2);  
  163.                 String word3 = words[++i];  
  164.                 distance = new DistanceNode(word3);  
  165.                 right = new SentenceNode(direction, action, distance);//构造的右表达式  
  166.                 stack.push(new AndNode(left, right));//将新and表达式入栈  
  167.             }  
  168.             //从头开始解释的直接将三个单词组成简单句子,并压入栈中。  
  169.             else{  
  170.                 String word1 = words[i];  
  171.                 direction = new DirectionNode(word1);  
  172.                 String word2 = words[++i];  
  173.                 action = new ActionNode(word2);  
  174.                 String word3 = words[++i];  
  175.                 distance = new DistanceNode(word3);  
  176.                 left = new SentenceNode(direction, action, distance);  
  177.                 stack.push(left);//入栈  
  178.             }  
  179.         }  
  180.         this.node = (INode) stack.pop();//将最终的表达式从栈中弹出(其实就是整个抽象语法树)  
  181.     }  
  182.       
  183.     public String interpret(){  
  184.         return node.interpret();//解释抽象语法树  
  185.     }  
  186. }  
运行结果:

[plain] view plain copy
  1. 向上移动8再向右快速移动10再向左移动6  
四、模式优缺点

优点:

1、易于改变和扩展文法。由于解释器模式采用类来表示文法规则,可以通过继承来改变或扩展原有文法。

2、易于实现文法。定义抽象语法树中的各个节点的类实现相似,易于直接编写,也方便采用编译器或语法分析生成器自动生成。

2、方便添加新的解释表达式。你可以在表达式类上定义新的操作以支持优美打印或表达式类型检查,优化,代码生成等,通常结合Visitor模式更佳。

缺点:

1、复杂的文法难以维护。复杂的文法会导致类层次的庞大,从而难以管理。此时可以采用编译器或语法分析程序来实现语法的解释。