软件工程学习日志2025.11.24

一、学习基本信息

  • 学习时间:[填写具体学习日期]
  • 学习主题:解释器模式、迭代器模式的原理与实践
  • 学习目标:
    1. 理解两种设计模式的核心动机、结构组成及适用场景
    2. 掌握基于设计模式的代码设计与实现方法
    3. 能够运用设计模式解决实际业务问题(机器人指令解析、学生信息遍历)
      二、理论学习总结
      (一)解释器模式
  1. 核心动机:用于定义语言的文法规则,并构建一个解释器来解释该语言中的句子。适用于需要频繁解析特定格式指令或表达式的场景,如机器人控制、公式计算等。
  2. 模式结构:
    • 抽象表达式(AbstractExpression):定义解释器的统一接口,声明interpret方法。
    • 终结符表达式(TerminalExpression):实现文法中终结符的解释逻辑,处理不可再分的基本元素。
    • 非终结符表达式(CompositeExpression):实现文法中非终结符的解释逻辑,处理复合规则(如多个表达式的组合)。
    • 上下文(Context):存储文法映射关系、全局数据等,为解释过程提供支持。
  3. 核心优势:易于扩展新的文法规则(新增表达式类即可),解释逻辑与业务逻辑分离,提高代码可读性。

(二)迭代器模式

  1. 核心动机:提供一种统一的方式遍历聚合对象(如集合、列表)中的元素,而无需暴露聚合对象的内部结构。适用于需要多种遍历方式(如升序、降序)或隐藏聚合对象实现细节的场景。
  2. 模式结构:
    • 迭代器接口(Iterator):定义遍历相关的方法(hasNext、next、reset等)。
    • 具体迭代器(ConcreteIterator):实现迭代器接口,封装具体的遍历逻辑(如升序遍历、降序遍历)。
    • 聚合对象(Aggregate):存储元素集合,提供创建迭代器的方法。
  3. 核心优势:遍历逻辑与聚合对象解耦,支持同一聚合对象多种遍历方式,符合"单一职责原则"。

三、实验实践过程
(一)解释器模式:机器人指令解析

  1. 实验需求
    根据给定文法规则,将英文机器人指令解析为中文描述。例如:
  • 输入"up move 5",输出"向上移动5个单位"
  • 输入"down run 10 and left move 20",输出"向下移动10个单位再向左移动20个单位"
  1. 实现步骤

  2. 设计上下文类Context:维护英文与中文的映射关系(方向:up→向上、动作:move→移动等)。

  3. 定义抽象表达式类AbstractExpression:声明interpret抽象方法。

  4. 实现终结符表达式TerminalExpression:处理单个指令(direction action distance)的解析逻辑。

  5. 实现非终结符表达式CompositeExpression:处理"expression and expression"的复合指令解析。

  6. 客户端构建:通过字符串分割与递归构建表达式树,调用interpret方法完成解析。

  7. 关键代码片段
    // 复合表达式解释逻辑
    @Override
    public String interpret(Context context) {
    // 复合指令拼接:左侧结果 + "再" + 右侧结果
    return leftExpr.interpret(context) + "再" + rightExpr.interpret(context);
    }

// 客户端解析逻辑
private static AbstractExpression buildExpression(StringTokenizer tokenizer) {
String direction = tokenizer.nextToken();
String action = tokenizer.nextToken();
int distance = Integer.parseInt(tokenizer.nextToken());
AbstractExpression expr = new TerminalExpression(direction, action, distance);

// 递归处理复合指令
if (tokenizer.hasMoreTokens() && "and".equals(tokenizer.nextToken())) {
    expr = new CompositeExpression(expr, buildExpression(tokenizer));
}
return expr;

}

  1. 测试结果
  • 输入:up move 5 → 输出:向上移动5个单位
  • 输入:down run 10 and left move 20 → 输出:向下移动10个单位再向左移动20个单位
  • 输入:right move 3 and up run 7 and left move 1 → 输出:向右移动3个单位再向上移动7个单位再向左移动1个单位

(二)迭代器模式:学生信息遍历

  1. 实验需求
    使用迭代器模式实现44名学生信息的遍历,支持学号升序和降序两种排列方式。

  2. 实现步骤

  3. 定义学生实体类Student:封装学号、姓名、年龄属性。

  4. 设计迭代器接口StudentIterator:声明hasNext、next、reset方法。

  5. 实现具体迭代器:

    • AscendingIterator:按学号升序排序并遍历。
    • DescendingIterator:按学号降序排序并遍历。
  6. 实现聚合类StudentCollection:维护学生列表,提供获取两种迭代器的方法。

  7. 客户端测试:创建学生集合,通过不同迭代器完成遍历输出。

  8. 关键代码片段
    // 升序迭代器排序逻辑
    public AscendingIterator(List students) {
    this.students = new ArrayList<>(students);
    this.students.sort(Comparator.comparing(Student::getStudentId)); // 按学号升序
    this.index = 0;
    }

// 聚合类提供迭代器
public StudentIterator getDescendingIterator() {
return new DescendingIterator(students);
}

// 客户端遍历
StudentIterator descendingIt = collection.getDescendingIterator();
while (descendingIt.hasNext()) {
System.out.println(descendingIt.next());
}


 4. 测试结果
- 升序遍历:按学号从2023001到2023005依次输出学生信息。
- 降序遍历:按学号从2023005到2023001依次输出学生信息。
- 扩展支持:通过`addStudent`方法批量添加44名学生后,遍历逻辑无需修改,符合"开闭原则"。

 四、类图设计
 (一)解释器模式类图
classDiagram
    direction TB
    class AbstractExpression {
        <<abstract>>
        +interpret(context: Context): String
    }
    class TerminalExpression {
        -direction: String
        -action: String
        -distance: int
        +TerminalExpression(direction: String, action: String, distance: int)
        +interpret(context: Context): String
    }
    class CompositeExpression {
        -leftExpr: AbstractExpression
        -rightExpr: AbstractExpression
        +CompositeExpression(left: AbstractExpression, right: AbstractExpression)
        +interpret(context: Context): String
    }
    class Context {
        -directionMap: Map~String, String~
        -actionMap: Map~String, String~
        +Context()
        +getDirectionCN(dir: String): String
        +getActionCN(action: String): String
    }
    AbstractExpression <|-- TerminalExpression
    AbstractExpression <|-- CompositeExpression
    TerminalExpression o-- Context
    CompositeExpression o-- AbstractExpression

(二)迭代器模式类图
classDiagram
direction TB
class Student {
-studentId: String
-name: String
-age: int
+Student(studentId: String, name: String, age: int)
+getStudentId(): String
+getName(): String
+getAge(): int
+toString(): String
}
class StudentIterator {
<>
+hasNext(): boolean
+next(): Student
+reset(): void
}
class AscendingIterator {
-students: ListStudent
-index: int
+AscendingIterator(students: ListStudent)
+hasNext(): boolean
+next(): Student
+reset(): void
}
class DescendingIterator {
-students: ListStudent
-index: int
+DescendingIterator(students: ListStudent)
+hasNext(): boolean
+next(): Student
+reset(): void
}
class StudentCollection {
-students: ListStudent
+addStudent(student: Student): void
+getAscendingIterator(): StudentIterator
+getDescendingIterator(): StudentIterator
}
StudentIterator <|-- AscendingIterator
StudentIterator <|-- DescendingIterator
AscendingIterator o-- StudentCollection
DescendingIterator o-- StudentCollection
StudentCollection o-- ListStudent
五、问题与解决

  1. 解释器模式中复合指令嵌套处理:通过递归调用buildExpression方法,支持多层"a and b and c"格式指令,解决了单一层次解析的局限性。
  2. 迭代器模式中学号排序逻辑:利用Comparator实现学号字符串的自然排序,确保升序/降序的正确性,同时避免修改聚合对象的原始数据顺序。
  3. 类型转换异常处理:在解析机器人指令的距离参数时,通过Integer.parseInt转换字符串,若输入非整数会抛出异常,后续可通过添加参数校验优化。
posted @ 2025-11-24 21:25  仙人兵马俑  阅读(4)  评论(0)    收藏  举报