原版设计模式之责任链模式

Intent (定义)

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.(为了避免将发送者和接收者耦合,给每个接收者都有一次处理请求的机会。将接收者串成链,并沿着链传递请求,直到有对象处理此请求为止。

Motivation (举例)

在这里插入图片描述
The first object in the chain receives the request and either handles it or forwards it to the next candidate on the chain, which does likewise. The object that made the request has no explicit knowledge of who will handle it—we say the request has an implicit receiver.(链中的第一个对象接收请求并处理它,或者将其转发给链上的下一个候选对象,这也是一样的。发出请求的对象并不明确知道谁将处理它——我们说请求有一个隐式接收器。
Let’s assume the user clicks for help on a button widget marked “Print.” The button is contained in an instance of PrintDialog, which knows the application object it belongs to (see preceding object diagram). The following interaction diagram illustrates how the help request gets forwarded along the chain:(假设用户点击“打印”的按钮小部件以寻求帮助。该按钮包含在PrintDialog的实例中,这个实例知道它所属的应用程序对象(请参见前面的对象图)。下面的交互图说明了如何沿着链转发帮助请求:
在这里插入图片描述
In this case, neither aPrintButton nor aPrintDialog handles the request; it stops at anApplication, which can handle it or ignore it. The client that issued the request has no direct reference to the object that ultimately fulfills it.(当aPrintButton和aPrintDialog都不能处理请求时,请求就停止在anApplication,anApplication可以处理或忽略它。发出该请求的客户端没有直接引用最终实现该请求的对象。
To forward the request along the chain, and to ensure receivers remain implicit, each object on the chain shares a common interface for handling requests and for accessing its successor on the chain. For example, the help system might define a HelpHandler class with a corresponding HandleHelp operation. HelpHandler can be the parent class for candidate object classes, or it can be defined as a mixin class. (为了沿着链转发请求,并确保接收器保持隐藏,链上的每个对象共享一个公共接口,用于处理请求和访问链上的后继对象。例如,帮助系统可能定义一个有HandleHelp方法的HelpHandler类 HelpHandler可以是候选对象类的父类,也可以定义为mixin类。
Then classes that want to handle help requests can make HelpHandler a parent:(想处理帮助请求的类就可以继承HelpHandler
在这里插入图片描述
The Button, Dialog, and Application classes use HelpHandler operations to handle help requests. HelpHandler’s HandleHelp operation forwards the request to the successor by default. Subclasses can override this operation to provide help under
the right circumstances; otherwise they can use the default implementation to forward the request.(Button、Dialog和Application类使用HelpHandler类的操作来处理帮助请求。默认情况下,HelpHandler的HandleHelp将请求转发给继任者。子类可以覆盖此操作,以便在正确的情况下提供帮助,否则它们可以使用默认实现来转发请求

Applicability (适用点)

  • more than one object may handle a request, and the handler isn’t known a priori. The handler should be ascertained automatically.(多个对象可以处理一个请求,并且该处理程序事先并不知道是哪种请求。处理程序应自动确定是否是自己需要处理的请求。
  • you want to issue a request to one of several objects without specifying the receiver explicitly.(您希望向几个对象中的一个发出请求,而不需要显式地指定接收器。
  • the set of objects that can handle a request should be specified dynamically.(动态地指定可以处理请求的对象集。

Structure (结构)

在这里插入图片描述
A typical object structure might look like this:(典型的结构如同这样
在这里插入图片描述

Participants (参与者)

  • Handler (HelpHandler)(处理者
    • defines an interface for handling requests. (定义处理请求的方法
    • (optional) implements the successor link.(【可选】实现后继者链接
  • ConcreteHandler (PrintButton, PrintDialog)(具体处理者
    • handles requests it is responsible for. (处理负责的请求
    • can access its successor. (可以访问他的继任者
    • if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor.(如果ConcreteHandler 可以处理请求,它就处理。否则就转交给下一位。
  • Client
    • initiates the request to a ConcreteHandler object on the chain.(发起请求,让链上的具体处理者对象处理

Collaborations(约定)

When a client issues a request, the request propagates along the chain until a ConcreteHandler object takes responsibility for handling it.(当客户端发出请求时,该请求将沿着链传播,直到有ConcreteHandler对象处理它为止。

Related Patterns (相关模式)

Chain of Responsibility is often applied in conjunction with Composite (183). There, a component’s parent can act as its successor.(责任链模式通常和组合模式一起使用。这样,组件的父类就可以当做它的后继者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值