最病 E Implementation n How intelligent should a command be 口仅确定一个接收者和执行该请求的动作 口自己实现所有功能,不需要额外的接收者 支持undo和redo:< oncrete Command需要存储额外 的状态信息(思考题:需要存储哪些状态?单级 和多级的undo和redo分别如何实现?) 口补偿式/反操作式;存储恢复式 口避免undo操作中的错误积累,有必要存入更多的 信息以保证能精确复原。 Institute of Computer Software 2021/215 Nanjing University
Implementation How intelligent should a command be? 仅确定一个接收者和执行该请求的动作 自己实现所有功能,不需要额外的接收者 支持undo和redo:ConcreteCommand需要存储额外 的状态信息(思考题:需要存储哪些状态?单级 和多级的undo和redo分别如何实现?) 补偿式/反操作式;存储恢复式 避免undo操作中的错误积累,有必要存入更多的 信息以保证能精确复原。 2021/2/5 Institute of Computer Software Nanjing University 26
最病 Related patterns 口 Composite模式可用来实现 command组合 口为实现undo/redo,可以用其他行为模式来管理状 态,如 Memento模式。C。 mmand被放到 history list 之前,可以用Proo↑γpe模式复制自身 Institute of Computer Software 2021/215 Nanjing University
Related Patterns Composite模式可用来实现command组合 为实现undo/redo,可以用其他行为模式来管理状 态,如Memento模式。Command被放到history list 之前,可以用Prototype模式复制自身 2021/2/5 Institute of Computer Software Nanjing University 27
最病 A Interpreter 口 ntent 口给定一个语言,定义它的文法的一种表示,并定义 个解释器,这个解释器使用该表示来解释语言中 的句子。 口 Motivation 口如果一种特定类型的问题发生的频率足够高,那么 可能就值得将该问题的各个实例表述为一个简单语 中的句子。这样就可以构建一个解释器,该解释 器通过解释这些句子来解决该问题。解释器模式描 述了如何为简单的语言定义一个文法并解释。 Institute of Computer Software 2021/215 Nanjing University
Interpreter Intent 给定一个语言,定义它的文法的一种表示,并定义 一个解释器,这个解释器使用该表示来解释语言中 的句子。 Motivation 如果一种特定类型的问题发生的频率足够高, 那么 可能就值得将该问题的各个实例表述为一个简单语 言中的句子。这样就可以构建一个解释器, 该解释 器通过解释这些句子来解决该问题。解释器模式描 述了如何为简单的语言定义一个文法并解释。 2021/2/5 Institute of Computer Software Nanjing University 28
最病 Exampl e expression: literal alternation sequence repetition expression alternation: =expression expression sequence: =expression expression repetition: = expression i* literal∷:'a'|"b|'c|…{a'|'b'c|…} Institute of Computer Software 2021/215 Nanjing University
Example 2021/2/5 Institute of Computer Software Nanjing University 29 expression ::= literal | alternation | sequence | repetition | '(' expression ')‘ alternation ::= expression '|' expression sequence ::= expression '&' expression repetition ::= expression '*' literal ::= 'a' | 'b' | 'c' | ... { 'a' | 'b' | 'c' | ... }*
最病 Exampl e RegularExpression Interpret( LiteralExpression SequenceExpression Expression expression2 Interpreto Interpreto literal repetition RepetitionExpression AlternationExpression kaiternatve1 alternative2 Interpreto Interpreto) Institute of Computer Software 2021/215 Nanjing University
Example 2021/2/5 Institute of Computer Software Nanjing University 30