责任链模式:拥有相互依赖的处理程序是一个好习惯吗?
我有这样的场景:我有一系列查询处理程序,第一个是查询缓存,如果缓存无法回答或答案已过时,则访问数据库,如果找不到答案或答案又过时了,然后查询远程 Web 服务。
但我不确定这是否是使用此模式的正确方法,因为工作流程几乎是固定的,并且缓存和数据库处理程序依赖于下一步的返回结果来刷新其记录。
I have this scenario: I have a chain of query handlers, the first is to query the cache, if the cache can't answer it or the answer is stale, then hit a database, if it can't find the answer or the answer is stale again, then query a remote web service.
But I am not sure if this is the right way to use this pattern, since the work flow is pretty much fixed, and the cache and database handlers depend on the next step's return result to refresh its records.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
严格来说,您所说的不是责任链模式。听起来更像是您正在实现缓存。如果您正在实现缓存,那么您描述的场景就可以了。但如果您正在寻找真正的责任链,则事实并非如此。
What you're referring to is not, strictly speaking, the chain of responsibility pattern. It sounds more like you're implementing a cache. If you are implementing a cache, then the scenario you describe is fine. If you're looking for true chain of responsibility though, it isn't.
您可以实现一个简单的继承层次结构,而不是像这样的链。
You could implement a simple inheritance hierarchy instead of chain like this.