Implementation 1902 26 How intelligent should a command be? 口仅确定一个接收者和执行该请求的动作 口自己实现所有功能,不需要额外的接收者 口支持undo和redo:Concrete Command需要存储额外 的状态信息(思考题:需要存储哪些状态?单级 和多级的undo和redo分别如何实现?) 0 避免undo操作中的错误积累,有必要存入更多的 信息以保证能精确复原。 口使用C++模板:针对不能取消且不需要参数的 命令 Institute of Computer Software 2022-2-27 Nanjing University
Implementation How intelligent should a command be? 仅确定一个接收者和执行该请求的动作 自己实现所有功能,不需要额外的接收者 支持undo和redo:ConcreteCommand需要存储额外 的状态信息(思考题:需要存储哪些状态?单级 和多级的undo和redo分别如何实现?) 避免undo操作中的错误积累,有必要存入更多的 信息以保证能精确复原。 使用C++模板:针对不能取消且不需要参数的 命令 2022-2-27 Institute of Computer Software Nanjing University 26
&扇 Related Patterns 27 ☐ Composite模式可用来实现command组合 o为实现undo/redo,可以用其他行为模式来管理状 态,如Memento模式。Command被放到history list 之前,可以用Prototype模式复制自身 Institute of Computer Software 2022-2-27 Nanjing University
Related Patterns Composite模式可用来实现command组合 为实现undo/redo,可以用其他行为模式来管理状 态,如Memento模式。Command被放到history list 之前,可以用Prototype模式复制自身 2022-2-27 Institute of Computer Software Nanjing University 27
Interpreter 102 28 Intent 给定一个语言,定义它的文法的一种表示,并定义 一个解释器,这个解释器使用该表示来解释语言中 的句子。 Motivation 口如果一种特定类型的问题发生的频率足够高,那么 可能就值得将该问题的各个实例表述为一个简单语 言中的句子。这样就可以构建一个解释器,该解释 器通过解释这些句子来解决该问题。解释器模式描 述了如何为简单的语言定义一个文法并解释。 Institute of Computer Software 2022-2-27 Nanjing University
Interpreter Intent 给定一个语言,定义它的文法的一种表示,并定义 一个解释器,这个解释器使用该表示来解释语言中 的句子。 Motivation 如果一种特定类型的问题发生的频率足够高, 那么 可能就值得将该问题的各个实例表述为一个简单语 言中的句子。这样就可以构建一个解释器, 该解释 器通过解释这些句子来解决该问题。解释器模式描 述了如何为简单的语言定义一个文法并解释。 2022-2-27 Institute of Computer Software Nanjing University 28
&扇 Example 29 expression :literal alternation sequence repetition |' expression ') alternation :expression 'expression sequence :expression '&expression repetition ::expression ' literal='a'|'b'|'c'|.{'a'|'b'|'c|.}* Institute of Computer Software 2022-2-27 Nanjing University
Example 2022-2-27 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' | ... }*
&雪扇 Example 102 UNIVE 30 RegularExpression interpret() LiteralExpression expression1 SequenceExpression expression2 Interpret() Interpret() literal repetition RepetitionExpression alternative1 AlternationExpression alternative2 Interpret() Interpret() Institute of Computer Software 2022-2-27 Nanjing University
Example 2022-2-27 Institute of Computer Software Nanjing University 30