c# - Which design pattern best describes the behavior in my example?
问题描述
The following fake example is a class that facilitates a polling process, stores the results, and saves it to a DB. My understanding is that this would be considered a Mediator pattern as it is facilitating the communication between objects that would otherwise be coupled without it. Is this accurate or would there be another pattern that explains this behavior better? Thank you!
public class StatusPoller
{
public void Poll()
{
foreach(User user in users)
{
string accessToken = oAuthClient.Authenticate(user); // Authenticate
List<status> statuses = pollingClient.PollStatus(accessToken); // Poll for status
Store(statuses); // Store statuses in DB
}
Message.Dispatch(5) // Dispatch a new message on a delay
}
}
解决方案
有点儿,
中介者模式要解决的问题是
We want to design reusable components, but dependencies between the potentially reusable pieces demonstrates the "spaghetti code" phenomenon (trying to scoop a single serving results in an "all or nothing clump").
所以什么时候StatusPoller
注入和处理它的所有依赖项,比如
oAuthClient
pollingClient
和dbContext
(可能在Store
方法中)
我们正在降低所有这些对象之间通信的复杂性。
中介者模式用于降低多个对象或类之间的通信复杂性。该模式提供了一个中介类,该类通常处理不同类之间的所有通信,并通过松散耦合支持轻松维护代码。
所以基本上你StatusPoller
是作为一个具体的调解人
推荐阅读
- emotion-js - 如何检查道具以在 Emotion 中的样式对象上输出代码块
- google-sheets - 谷歌表格:是否可以简化这个公式?最好,我希望它计算行中的所有单元格
- python - 使用 python 搜索一个模棱两可的 Outlook 收件人
- python - Accuracy 和 Recall 值相同
- canvas - 画布drawimage问题,导致白色,仅某些图像
- haskell - Haskell IO 动作序列处理,实时过滤它们的结果+在某些时刻执行一些 IO 动作
- python - 如何使用标称数据类型预测多项式回归 Python
- python - 如何在python中将字符串转换为getpass.getpass()字符串
- python - 如何遍历 pandas 数据框并访问滞后行或前导行?
- angular6 - 在 routerLink 中传递自定义对象