责任模式链 [英] The Chain of Responsibility Pattern

查看:184
本文介绍了责任模式链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以简单地解释责任模式链吗?我发现wiki文章有点混乱。

解决方案

一个很好的例子是 java servlet过滤器 - 在HTTP请求到达目标之前执行的代码段。




  • 链包含多个实例,每个实例都执行不同的操作

  • 链中的每个实例可以选择传播到下一个例子,或者停止流程



所以,使用servlet过滤器,你可以有




  • 一个过滤器,用于检查用户是否经过身份验证。如果他是,则过滤器传播到下一个过滤器


  • 下一个过滤器检查用户是否具有当前资源的权限。如果是这样,它传播到下一个


  • 下一个日志记录当前的请求URL和用户名,并始终传播到下一个


  • 链中没有其他的东西,所以最终调用目标对象



Could somebody provide a simple explanation of the chain of responsibility pattern? I found the wiki article a bit confusing.

解决方案

A very good example are java servlet filters - pieces of code that are executed before the HTTP request arrives at its target.

  • the chain contains multiple instances, and each of them performs a different action
  • each instance in the chain can choose to propagate to the next instance, or stop the flow

So, with servlet filters, you can have

  • a filter that checks if the user is authenticated. If he is, the filter propagates to the next filter

  • the next filter checks if the user has permissions to the current resource. If it does, it propagate to the next

  • the next logs the current request URL and the username, and always propagate to the next

  • there is nothing else in the chain, so the target object is finally invoked

这篇关于责任模式链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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