行为型软件设计模式

行为模式涉及到算法和对象间职责的分配\r\nare concerned with algorithms and the assignment of responsibilities between objects.\r\ndescribe the patterns of communication between objects or classes.\r\ncharacterize complex control flow.(刻划了在运行时难以跟踪的复杂的控制流)\r\n行为类模式:使用继承在类间分配行为。\r\n行为对象模式:对象之间如何相互协作以完成单个对象无法完成的任务。\r\n行为模式(11种)

Mediator(中介者)\r\nMemento(备忘录)\r\nObserver(观察者)\r\nState(状态)\r\nStrategy(策略)\r\nTemplate method(模板方法)\r\nVisitor(访问者)

ChainOf Responsibility(职责链)\r\n Command(命令)\r\n Interpreter(解释器)\r\n Iterator(迭代器)

5.1 Chain Of Responsibility(职责链) –对象行为型模式

意图\r\n使多个对象都有机会处理请求。\r\nAvoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.\r\nChain the receiving objects and pass the request along the chain until an object handles it.

5.1 Chain Of Responsibility(职责链) –对象行为型模式

动机\r\n一个图形用户界面中的上下文有关的帮助机制。\r\n用户在界面的任何部位上点击就可得到帮助信息。\r\n所得到的帮助信息依赖于点击的是界面的哪一部分及其上下文。\r\n在众多界面对象中必须有一个来处理帮助请求\r\n是谁呢?\r\n帮助信息的组织:从最具体到最一般。

Motivation

将处理对象连成一条链,请求沿链传递,直至被处理.\r\n客户不知道具体的处理对象是谁.

要求:\r\n每个在链上的对象都有一致的处理请求和访问链上后继者的接口。\r\n如上图,按钮、对话框和应用类都使用HelpHandler操作处理帮助请求。\r\n缺省实现是将请求转发给后继者。\r\n子类可重定义该操作提供处理。

5.1 Chain Of Responsibility(职责链) –对象行为型模式

适用性\r\n有多个对象可以处理同一请求,具体由谁处理需要在运行时刻确定。\r\n你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。\r\nYou want to issue a request to one of several objects without specifying the receiver explicitly.\r\n可处理一个请求的对象集合应被动态指定。\r\nThe set of objects that can handle a request should be specified dynamically.

结构

Handler\r\n定义一个处理请求的接口。\r\n实现后继链 (可选)\r\nConcreteHandler\r\n处理它所负责的请求\r\n可访问其后继者\r\nforward to successor if it can't handle the request\r\nClient\r\ninitiates the request to the first ConcreteHandler in the chain.

典型的对象结构

5.1 Chain Of Responsibility(职责链) –对象行为型模式

协作\r\n当客户提交一个请求时,请求沿链传递直至有一个ConcreteHandler对象负责处理它。

5.1 Chain Of Responsibility(职责链) –对象行为型模式

效果\r\n降低耦合度。\r\nreceiver and sender have no explicit knowledge of each other\r\ncan simplify object interactions\r\n增强了给对象指派职责的灵活性。\r\ncan add or change responsibilities by changing the chain at run-time.\r\n不保证被接受。\r\nrequest may fall off the end of the chain

5.1 Chain Of Responsibility(职责链) –对象行为型模式

实现\r\n实现后继者链\r\n定义新的链接\r\n使用已有的链接\r\n连接后继者。\r\n缺省实现:向后继者转发请求。\r\n表示请求。\r\n硬编码的操作调用\r\n一个处理函数,参数为一个请求码。\r\n需要用条件语句来区分请求代码。\r\n无法用类型安全的方法传递参数。\r\n使用独立的请求对象来封装请求参数。\r\n分派函数 P151 \r\n子类可通过重定义HandleRequest扩展该分派函数。

5.1 Chain Of Responsibility(职责链) –对象行为型模式

代码示例\r\nP152-154。\r\n相关模式\r\n职责链常与Composite一起使用。\r\n一个构件的父构件可作为它的后继。

5.2 Command(命令) –对象行为型模式

意图\r\n将一个请求封装为一个对象,使得:\r\n可用不同的请求对客户进行参数化\r\nparameterize clients with different requests\r\n对请求排队或记录请求日志\r\nqueue or log requests\r\n支持可撤销的操作\r\nsupport undoable operations\r\n别名\r\n动作,事务

5.2 Command(命令) –对象行为型模式

动机\r\n图形按钮和菜单\r\n用来响应用户输入,执行请求。\r\n工具箱的设计者无法知道请求的接收者或可执行的操作。\r\n只有使用工具箱的应用知道该由哪个对象做那个操作。

5.2 Command(命令) –对象行为型模式

动机\r\n命令模式\r\n通过将请求本身变成一个对象来使工具箱对象可向未指定的应用对象提出请求。\r\n这个对象可被存储并像其他对象一样被传递。

每一个菜单中的选项都是一个菜单项MenuItem的实例。\r\n一个Application实例创建这些菜单和相应菜单项以及其它用户界面,跟踪打开的Document对象。\r\n该应用为每个菜单项配置一个具体的Command子类的实例\r\n当用户选择一个菜单项时,该MenuItem对象调用它的Command对象的Execute方法,而Execute执行相应操作。\r\nMenuItem对象并不知道他们使用的是Command的哪个子类。Command子类里存放着请求的接收者,而Execute操作调用该接收者的一个或多个操作。

例1:PasteCommand的接收者是一个文档对象,该对象是实例化时提供的。

例2:OpenCommand的接收对象是Application实例。

复合命令:或称宏命令。\r\nMacroCommand是一个具体的Command子类,它执行一个命令序列。\r\nMacroCommand没有明确的接收者,而序列中的命令各自定义自己的接收者。

5.2 Command(命令) –对象行为型模式

适用性\r\nparameterize objects by an action to perform,代替回调。\r\nCommand模式是回调机制的一个面向对象的替代品。\r\n在不同时刻指定、排列和执行请求。\r\nspecify, queue, and execute requests at different times 。\r\n一个Command对象可以有一个与初始请求无关的生存期。\r\nsupport undo\r\nCommand接口添加Unexecute 操作。\r\n保存当前状态。\r\n建立历史命令列表。

5.2 Command(命令) –对象行为型模式

适用性\r\n支持修改日志,当系统崩溃时,这些修改可重做一遍。\r\nsupport logging changes so that they can be reapplied in case of a system crash。\r\n用构建在原语操作上的高层操作构造一个系统\r\nstructure a system around high-level operations built on primitives operations —— transactions\r\nCommand模式提供了对事务进行建模的方法。

结构

create

set

Holds command

Transforms:   concreteReceiver.action() in command.execute()

5.2 Command(命令) –对象行为型模式

参与者\r\nCommand\r\n声明执行操作的接口。\r\nConcreteCommand(PasteCommand)\r\n将一个接收者对象绑定于一个动作\r\n调用接收者相应操作,以实现Execute。\r\nClient(Application)\r\n创建一个具体命令对象并设定它的接收者。\r\nInvoker(MenuItem)\r\n要求该命令执行这个请求。\r\nReceiver(Document,Application)\r\n知道如何实施与执行一个请求相关的操作。任何类都可能作为一个接收者。

协作

5.2 Command(命令) –对象行为型模式

效果\r\n1、Command模式将调用操作的对象与知道如何实现该操作的对象解耦。\r\nDecouples Invoker from Receiver\r\n2、Command是头等对象。\r\nCommands are first-class objects\r\ncan be manipulated and extended\r\n3、可将多个命令装配成一个复合命令\r\n4、增加新的Command子类很容易。\r\nPotential for an excessive number of command classes

5.2 Command(命令) –对象行为型模式

实现\r\n1、一个命令对象应达到何种智能程度\r\n“Dumb”:仅确定一个接收者和执行该请求的动作\r\ndelegate everything to Receiver\r\nused just to decouple Sender from Receiver\r\n“Genius”:自己实现所有功能\r\ndoes everything itself without delegating at all\r\nuseful if no receiver exists\r\nlet ConcreteCommand be independent of further classes\r\n“Smart”:介于上面两个极端之间\r\nfind receiver dynamically

5.2 Command(命令) –对象行为型模式

实现\r\n2、支持取消(Undo)和重做(Redo)\r\nNeed to store additional state to reverse execution\r\nreceiver object\r\nparameters of the operation performed on receiver\r\noriginal values in receiver that may change due to request\r\nreceiver must provide operations that makes possible for command object to return it to its prior state\r\nHistory list:支持多级的取消和重做\r\nsequence of commands that have been executed \r\nused as LIFO with reverse-execution ? undo\r\nused as FIFO with execution ? redo\r\nCommands may need to be copied\r\nwhen state of commands change by execution

5.2 Command(命令) –对象行为型模式

实现\r\n3、避免取消操作过程中的错误积累

5.2 Command(命令) –对象行为型模式

实现\r\n4、使用C   模板实现:\r\n既不能被取消,也不需要参数的简单命令\r\nWorks only for simple commands!\r\nif action needs parameters or command must keep state use a Command subclass!\r\nP160 代码

5.2 Command(命令) –对象行为型模式

代码示例\r\nP159—P161

5.2 Command(命令) –对象行为型模式

相关模式\r\nComposite模式可用来实现command组合\r\n为实现undo/redo,可以用其他行为模式来管理状态,如memento模式。Command被放到history list之前,可以用prototype模式复制自身

5.3 Interpreter(解释器) –类行为型模式

意图\r\n给定一种语言,定义它的一种文法表示。定义一个解释器,这个解释器使用该表示来解释语言中的句子。\r\nGiven a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

5.3 Interpreter(解释器) –类行为型模式

动机\r\n如果一种特定类型的问题发生频率很高,就值得将该问题的各个实例表达成一个简单语言的句子。再构造一个解释器,通过解释这些句子来解决具体问题。\r\n常见问题:搜索匹配一个模式的字符串。\r\n用正则表达式表示待匹配的字符串集合。\r\n使用通用搜索算法

5.3 Interpreter(解释器) –类行为型模式

动机\r\n解决3个方面的问题\r\n1、如何为简单的语言定义一个文法?\r\n例子:为正则表达式定义一个文法。\r\n2、如何在该语言中表示一个句子?\r\n例子:如何表示一个具体的正则表达式。\r\n3、如何解释这些句子?\r\n例子:如何解释这个具体的正则表达式。

5.3 Interpreter(解释器) –类行为型模式

动机\r\n第1个问题:正则表达式的文法

expression ::= literal | alternation | sequence | repetition |\r\n'(' expression ')'\r\nalternation ::= expression '|' expression\r\nsequence ::= expression '