责任模式链 [英] The Chain of Responsibility Pattern
问题描述
一个很好的例子是 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屋!