首页 > 解决方案 > 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
    }
}

标签: c#oopdesign-patternsmediator

解决方案


有点儿,

中介者模式要解决的问题是

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 pollingClientdbContext(可能在Store方法中)

我们正在降低所有这些对象之间通信的复杂性。

中介者模式用于降低多个对象或类之间的通信复杂性。该模式提供了一个中介类,该类通常处理不同类之间的所有通信,并通过松散耦合支持轻松维护代码。

所以基本上你StatusPoller是作为一个具体的调解人


推荐阅读