责任链模式:拥有相互依赖的处理程序是一个好习惯吗?

发布于 2024-09-01 17:43:52 字数 159 浏览 2 评论 0原文

我有这样的场景:我有一系列查询处理程序,第一个是查询缓存,如果缓存无法回答或答案已过时,则访问数据库,如果找不到答案或答案又过时了,然后查询远程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-09-08 17:43:52

严格来说,您所说的不是责任链模式。听起来更像是您正在实现缓存。如果您正在实现缓存,那么您描述的场景就可以了。但如果您正在寻找真正的责任链,则事实并非如此。

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.

旧竹 2024-09-08 17:43:52

您可以实现一个简单的继承层次结构,而不是像这样的链。

Interface
+getData()
^
|
RemoteWebserviceImpl
^
|
DatabaseImpl
^
|
CacheImpl

You could implement a simple inheritance hierarchy instead of chain like this.

Interface
+getData()
^
|
RemoteWebserviceImpl
^
|
DatabaseImpl
^
|
CacheImpl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文