zoukankan      html  css  js  c++  java
  • 软件设计解释器模式

    某机器人控制程序包含一些简单的英文指令,其文法规则如下:

    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个单位

    package 实验17解释器模式;
    
    
    abstract class AbstractNode {
        public abstract String interpret();
    }
    AbstractNode
    package 实验17解释器模式;
    
    public class ActionNode extends AbstractNode{
        private String action;
    
        public ActionNode(String action) {
            super();
            this.action = action;
        }
    
        @Override
        public String interpret() {
            // TODO Auto-generated method stub
            if(action.equals("move")){
                return "移动";
            }else if(action.equals("run")){
                return "快速移动";
            }else{
                return "error";
            }
            
        }
    }
    ActionNode
    package 实验17解释器模式;
    
    public class AndNode extends AbstractNode {
        private AbstractNode left;
        private AbstractNode right;
        @Override
        public String interpret() {
            // TODO Auto-generated method stub
            return left.interpret() + "再" +right.interpret();
        }
        public AndNode(AbstractNode left, AbstractNode right) {
            super();
            this.left = left;
            this.right = right;
        }
    }
    AndNode
    package 实验17解释器模式;
    
    public class Client {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String s = "down run 10 and left move 20";
            InstructionHandler handler = new InstructionHandler();
            handler.handle(s);
            
            System.out.println(handler.output());
    
        }
    
    }
    Client
    package 实验17解释器模式;
    
    public class DirectionNode extends AbstractNode{
        private String direction;
        
        public DirectionNode(String direction) {
            super();
            this.direction = direction;
        }
        @Override
        public String interpret() {
            // TODO Auto-generated method stub
            if(direction.equals("up")){
                return "向上";
            }else if(direction.equals("down")){
                return "向下";
            }else if(direction.equals("left")){
                return "向左";
            }else if(direction.equals("right")){
                return "向右";
            }else{
                return "error";
            }
        }
    }
    DirectionNode
    package 实验17解释器模式;
    
    public class DistanceNode extends AbstractNode{
        private String distance;
    
        public DistanceNode(String dis) {
            super();
            this.distance = dis;
        }
    
        @Override
        public String interpret() {
            // TODO Auto-generated method stub
            return this.distance;
        }
        
    }
    DistanceNode
    package 实验17解释器模式;
    
    import java.util.Stack;
    
    public class InstructionHandler {
        private AbstractNode node;
        public void handle(String instruction){
            AbstractNode left = null;
            AbstractNode right = null;
            AbstractNode direction = null;
            AbstractNode action = null;
            AbstractNode distance = null;
            Stack<AbstractNode> 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 dir = words[++i];
                    direction = new DirectionNode(dir);
                    String a = words[++i];
                    action = new ActionNode(a);
                    String dis = words[++i];
                    distance = new DistanceNode(dis);
                    right = new SentenceNode(direction, action, distance);
                    stack.push(new AndNode(left, right));
                }else{
                    String dir = words[i];
                    direction = new DirectionNode(dir);
                    String a = words[++i];
                    action = new ActionNode(a);
                    String dis = words[++i];
                    distance = new DistanceNode(dis);
                    left = new SentenceNode(direction, action, distance);
                    stack.push(left);
                    
                }
            }
            this.node = (AbstractNode)stack.pop();
        }
        public String output(){
            String result = node.interpret();
            return result;
        }
    
    }
    InstructionHandler
    package 实验17解释器模式;
    
    public class SentenceNode extends AbstractNode{
        private AbstractNode direction;
        private AbstractNode action;
        private AbstractNode distance;
        public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance) {
            super();
            this.direction = direction;
            this.action = action;
            this.distance = distance;
        }
        @Override
        public String interpret() {
            // TODO Auto-generated method stub
            return direction.interpret()+action.interpret()+distance.interpret();
        }
    
    }
    SentenceNode
  • 相关阅读:
    图片滤镜高斯模糊
    context.Request.Files为NULL问题 在实现图片上传功能的时候出现在ashx等处理页面出现context.Request.Files为NULL异常,有几点需要注意:
    C#中使用代码动态改变配置文件信息
    缓存
    使用iframe实现图片上传预览效果
    loading 加载
    sql 查询每月的销售金额
    GridView数据格式化
    把图片转换成二进制--把二进制转换成图片
    asp.net js 倒计时总秒数量 和 排序
  • 原文地址:https://www.cnblogs.com/feng747/p/15574864.html
Copyright © 2011-2022 走看看