```mermaid classDiagram class Expression { <<interface>> +interpret() String } class Direction { -String direction +interpret() String } class Action { -String action +interpret() String } class Distance { -int distance +interpret() String } class SimpleExpression { -Expression direction -Expression action -Expression distance +interpret() String } class CompositeExpression { -List~Expression~ expressions +addExpression(Expression expression) void +interpret() String } class InterpreterContext { +parse(String input) Expression } Expression <|.. Direction Expression <|.. Action Expression <|.. Distance Expression <|.. SimpleExpression Expression <|.. CompositeExpression InterpreterContext --> CompositeExpression CompositeExpression o-- Expression ```
package Tutorial17; import java.util.ArrayList; import java.util.List; // 1. Expression接口,定义解析接口 interface Expression { String interpret(); } // 2. Direction、Action和Distance表达式类,用于解释基本指令 class Direction implements Expression { private String direction; public Direction(String direction) { this.direction = direction; } @Override public String interpret() { return switch (direction) { case "up" -> "向上"; case "down" -> "向下"; case "left" -> "向左"; case "right" -> "向右"; default -> throw new IllegalArgumentException("无效的方向: " + direction); }; } } class Action implements Expression { private String action; public Action(String action) { this.action = action; } @Override public String interpret() { return switch (action) { case "move" -> "移动"; case "run" -> "奔跑"; default -> throw new IllegalArgumentException("无效的动作: " + action); }; } } class Distance implements Expression { private int distance; public Distance(int distance) { this.distance = distance; } @Override public String interpret() { return distance + "个单位"; } } // 3. SimpleExpression类,解释单一的方向-动作-距离指令 class SimpleExpression implements Expression { private Expression direction; private Expression action; private Expression distance; public SimpleExpression(Expression direction, Expression action, Expression distance) { this.direction = direction; this.action = action; this.distance = distance; } @Override public String interpret() { return direction.interpret() + action.interpret() + distance.interpret(); } } // 4. CompositeExpression类,用于处理“and”复合表达式 class CompositeExpression implements Expression { private List<Expression> expressions = new ArrayList<>(); public void addExpression(Expression expression) { expressions.add(expression); } @Override public String interpret() { StringBuilder result = new StringBuilder(); for (int i = 0; i < expressions.size(); i++) { result.append(expressions.get(i).interpret()); if (i < expressions.size() - 1) { result.append("再"); } } return result.toString(); } } // 5. 解释器上下文类,解析并生成对应的表达式 class InterpreterContext { public Expression parse(String input) { String[] parts = input.split(" and "); CompositeExpression composite = new CompositeExpression(); for (String part : parts) { String[] tokens = part.split(" "); Expression direction = new Direction(tokens[0]); Expression action = new Action(tokens[1]); Expression distance = new Distance(Integer.parseInt(tokens[2])); composite.addExpression(new SimpleExpression(direction, action, distance)); } return composite; } } // 6. 测试类 public class Main { public static void main(String[] args) { InterpreterContext context = new InterpreterContext(); String input1 = "up move 5"; Expression expression1 = context.parse(input1); System.out.println("输入: " + input1 + ",输出: " + expression1.interpret()); String input2 = "down run 10 and left move 20"; Expression expression2 = context.parse(input2); System.out.println("输入: " + input2 + ",输出: " + expression2.interpret()); } }
标签:distance,direction,String,软件设计,Tutorial18,action,Expression,interpret From: https://www.cnblogs.com/muzhaodi/p/18543020