实验17:解释器模式(选作)
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解解释器模式的动机,掌握该模式的结构;
2、能够利用解释器模式解决实际问题。
[实验任务一]:解释器模式
某机器人控制程序包含一些简单的英文指令,其文法规则如下:
expression ::= direction action distance | composite
composite ::= expression and expression
direction ::= ‘up’ | ‘down’ | ‘left’ | ‘right’
action ::= ‘move’ | ‘run’
distance ::= an integer //一个整数值
如输入:up move 5,则输出“向上移动5个单位”;输入:down run 10 and left move 20,则输出“向下移动10个单位再向左移动20个单位”。
实验要求:
1、类图
2、源代码
①、AbstractNode.java
package org.example.shiyanshiqi;
public abstract class AbstractNode {
public abstract String interpret();
}
②、AndNode.java
package org.example.shiyanshiqi;
public class AndNode extends AbstractNode{
private AbstractNode left;
private AbstractNode right;
public AndNode(AbstractNode left, AbstractNode right) {
this.left = left;
this.right = right;
}
@Override
public String interpret() {
return left.interpret() + " 再 " + right.interpret();
}
}
③、SentenceNode.java
package org.example.shiyanshiqi;
public class SentenceNode extends AbstractNode {
private AbstractNode direction;
private AbstractNode action;
private AbstractNode distance;
public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance) {
this.direction = direction;
this.action = action;
this.distance = distance;
}
@Override
public String interpret() {
return direction.interpret() + action.interpret() + distance.interpret();
}
}
④、DirectionNode.java
package org.example.shiyanshiqi;
public class DirectionNode extends AbstractNode {
private String direction;
public DirectionNode(String direction) {
this.direction = direction;
}
@Override
public String interpret() {
if (direction.equalsIgnoreCase("up")) {
return "向上";
} else if (direction.equalsIgnoreCase("down")) {
return "向下";
} else if (direction.equalsIgnoreCase("left")) {
return "向左";
} else if (direction.equalsIgnoreCase("right")) {
return "向右";
} else {
return "无效指令";
}
}
}
⑤、DistanceNode.java
package org.example.shiyanshiqi;
public class DistanceNode extends AbstractNode{
private String distance;
public DistanceNode(String distance){
this.distance=distance;
}
@Override
public String interpret() {
return this.distance;
}
}
⑥、ActionNode.java
package org.example.shiyanshiqi;
public class ActionNode extends AbstractNode {
private String action;
public ActionNode(String action) {
this.action = action;
}
@Override
public String interpret() {
if (action.equalsIgnoreCase("move")) {
return "移动";
} else if (action.equalsIgnoreCase("run")) {
return "快速移动";
} else {
return "停止";
}
}
}
⑦、InstructionHandler.java
package org.example.shiyanshiqi;
import java.util.*;
public class InstructionHandler {
private String instruction;
private AbstractNode node;
public void handle(String instruction){
AbstractNode left=null,right=null;
AbstractNode direction=null,action=null,distance=null;
Stack stack=new Stack();
String[] words=instruction.split(" ");
for(int i=0;i<words.length;i++){
if(words[i].equalsIgnoreCase("and")){
left=(AbstractNode)stack.pop();
String word1=words[++i];
direction=new DirectionNode(word1);
String word2=words[++i];
action = new ActionNode(word2);
String word3=words[++i];
distance=new DistanceNode(word3);
right=new SentenceNode(direction,action,distance);
stack.push(new AndNode(left,right));
}else{
String word1=words[i];
direction=new DirectionNode(word1);
String word2=words[++i];
action=new ActionNode(word2);
String word3=words[++i];
distance=new DistanceNode(word3);
left=new SentenceNode(direction,action,distance);
stack.push(left);
}
}
this.node=(AbstractNode) stack.pop();
}
public String output(){
return node.interpret();
}
}
⑧、Client.java
package org.example.shiyanshiqi;
public class Client {
public static void main(String[] args) {
String instruction="up move 6 and down run 11 and left move 6";
InstructionHandler handler=new InstructionHandler();
handler.handle(instruction);
String outString=handler.output();
System.out.println(outString);
}
}
3、运行结果