链责任设计模式与使用简单的if-elseif-else块有什么区别? [英] What is the difference between Chain Of Responsibility design pattern and using a simple if-elseif-else block?

查看:341
本文介绍了链责任设计模式与使用简单的if-elseif-else块有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚查找责任链,而且我发现了这个

I was just looking up Chain Of Responsibility the other day, and I came across this example.

基本上,有一个抽象处理程序,然后是具体的处理程序,每个处理程序都实现了父抽象处理程序的handle方法。实现是这样的,起初有一个检查,看看这个特定的处理程序是否可以处理当前的请求,如果没有,那么它会将请求传递给它的后继者。

Basically, there is an abstract handler, and then are concrete handlers, each of which implement the handle method of the parent abstract handler. The implementation is such that at first there is a check to see if this particular handler can process the current request, and if not, then it passes on the request to its successor.

现在,我也可以使用一个简单的if-else条件块来做同样的事情。要从上面的链接中获取第一个例子,这里是我如何更改它:

Now, I could also do the same thing using a simple if-else conditional block. To take the first example from the link above, here is how I would change it:

class SingleHandler
{
    if(request > 0 && request <= 10)
    {
        // Process request
    }
    else if(request > 10 && request <= 20)
    {
        // Process request differently
    }
    else if(request > 20 && request <= 30)
    {
        // Process request differently
    }
}

现在我的问题是,两者之间的根本区别是什么?有没有任何具体的理由,我应该使用责任链,如果我可以提供完全相同的功能使用if-else块?关于性能,内存消耗,可维护性,可扩展性,哪一个更好?

Now, my question is, what is the fundamental difference between the two? Is there any specific reason I should use Chain Of Responsibility at all, if I can provide the exact same functionality using if-else blocks? Which one is better with regards to performance, memory consumption, maintainability, scalability?

推荐答案

是的,你可以重写这个例子使用多个if-else级联。但只是因为这是一个很简单的例子。

Yes, you could re-write this example to use multiple if-else-cascades. But only because it's a rather simple example.

责任链是一种动态模式。这意味着处理程序可以在运行时交换。这通常在UI代码中完成,其中几个嵌套控件可以表示处理程序。想象下面的情况:

The Chain Of Responsibility is a dynamic pattern. That means that handlers can be exchanged during run-time. This is often done in UI code where several nested controls can represent the handlers. Imagine the following scenario:

你有一个窗口。在这个窗口中有一些面板。在这个面板中有一个文本框。右键单击文本框。执行的命令取决于层次结构。系统会询问第一个处理程序 - 文本框 - 来处理点击请求。如果它不知道该怎么处理请求,它会传递给它的父级 - 面板等等。我怀疑你想用if-else-cascade实现这种场景。每次更改UI时,您都必须更改级联。这就是为什么使用处理程序对象。它使代码可交换和可重用。

You have a window. In this window there is some kind of panel. In this panel there is a text box. You right-click the text box. The executed command depends on the hierarchy. The system would ask the first handler - the textbox - to handle the click-request. If it does not know what to do with the request, it passes it on to its parent - the panel - etc. I doubt that you want to implement this kind of scenario with an if-else-cascade. Every time you change the UI, you would have to change the cascade. That's why handler objects are used. It makes the code exchangable and re-usable.

许多模式可以以不同的方式实现。这是在没有面向对象的低级编程语言中的惯例。然而,这些代码通常是非常不灵活的,很难维护。然而,这是什么让他们快速。

Many patterns can be implemented in a different way. This is usual practice in low-level programming languages with no object-orientation. However, these codes are usually quite inflexible and hard to maintain. Yet, this is what makes them fast.

这篇关于链责任设计模式与使用简单的if-elseif-else块有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆