我为什么要使用责任链而不是装饰者?[英] Why would I ever use a Chain of Responsibility over a Decorator?

本文是小编为大家收集整理的关于我为什么要使用责任链而不是装饰者?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我只是在阅读责任链当我希望它的用法比 Decorator "> decorator .

您怎么看? Cor有利基使用吗?

推荐答案

您可以在任何时候打破链条的事实将责任模式与装饰链模式区分开来.可以将装饰师视为立即执行所有操作,而不会与其他装饰人员进行任何互动.可以将链中的链接视为一次执行一个,因为它们每个都取决于先前的链接.

当您可以将程序概念化为由链接组成的链条时,请使用责任链模式,每个链接都可以处理请求或将其传递到链条中.

当我与Win32 API合作时,有时我需要使用它提供的挂钩功能.挂起窗户消息大致遵循责任链模式.当您挂起WM_MouseMove之类的消息时,将调用您的回调函数.将回调函数视为链中的最后一个链接.链中的每个链接都可以决定是扔掉WM_Mousemove消息还是将其传递到下一个链接.

如果在该示例中使用了装饰器图案,您将收到WM_Mousemove消息的通知,但是您也可以防止其他挂钩处理它.

另一个位置在游戏引擎中使用的命令模式链.同样,您可以连接引擎功能,事件和其他东西.对于游戏引擎,您不想仅添加功能.您想添加功能并防止游戏引擎执行其默认操作.

其他推荐答案

避免耦合请求的发件人 通过给予超过超过的接收者 一个对象有机会处理 要求.链接收对象 并沿链条传递 直到对象处理它为止.

vs

装饰

将其他职责附加到 动态对象.装饰者 提供灵活的替代方案 分类用于扩展 功能.

我会说它围绕着事情发生的顺序.如果您将它们链接起来,则将沿链条拨打.使用装饰员您不能保证此订单,只能附加额外的责任.

其他推荐答案

我会说责任链是 demanter ..

的一种特殊形式

本文地址:https://www.itbaoku.cn/post/627663.html

问题描述

I'm just reading up on the Chain of Responsibility pattern and I'm having trouble imagining a scenario when I would prefer its use over that of decorator.

What do you think? Does CoR have a niche use?

推荐答案

The fact that you can break the chain at any point differentiates the Chain of Responsibility pattern from the Decorator pattern. Decorators can be thought of as executing all at once without any interaction with the other decorators. Links in a chain can be thought of as executing one at a time, because they each depend on the previous link.

Use the Chain of Responsibility pattern when you can conceptualize your program as a chain made up of links, where each link can either handle a request or pass it up the chain.

When I used to work with the Win32 API, I would sometimes need to use the hooking functionality it provides. Hooking a Windows message roughly follows the Chain of Responsibility pattern. When you hooked a message such as WM_MOUSEMOVE, your callback function would be called. Think of the callback function as the last link in the chain. Each link in the chain can decide whether to throw away the WM_MOUSEMOVE message or pass it up the chain to the next link.

If the Decorator pattern had been used in that example, you would have been notified of the WM_MOUSEMOVE message, but you would be powerless to prevent other hooks from handling it as well.

Another place the Chain of Command pattern is used is in game engines. Again, you can hook engine functions, events, and other things. In the case of a game engine, you don't want to simply add functionality. You want to add functionality and prevent the game engine from performing its default action.

其他推荐答案

Chain

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.

vs

Decorator

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

I'd say its around the order in which things will happen. If you chain them, the will be called along the chain. With a decorator you're not guaranteed this order, only that additional responsibilities can be attached.

其他推荐答案

I'd say that a Chain of Responsibility is a particular form of Decorator.