这是观察者反模式吗? (有奖金状态机问题)

发布于 2024-10-13 10:03:45 字数 1913 浏览 9 评论 0原文

我想知道以下是否是对观察者模式的错误使用。我知道主体应该是一个,而听众应该是多个。然而,我的应用程序中的主题最终可能比听众更多!

玩家

Form1:不言自明
DocumentCreator:包含工厂方法和从列表中选择文件的策略
Document:包含有关文档文件的信息和子项的模板方法

建议的

IErrorProne:为上述玩家实现事件的接口,将他们变成主题 报告:侦听 IErrorProne 对象并处理日志记录/电子邮件
DocumentState:这是一个我有点不确定的奖励。我还没有完全确定模板之外的良好流程。目前我在 Document 类中有一个状态机。我想将状态机从 Document 类中拉出并放入 Form1 中,从而将两者相互解耦。

public interface IErrorProne
{
    public delegate void ErrorEventDelegate(
        object sender, 
        ErrorEventArgs e
        );

    public event ErrorEventDelegate ReportError;
}

public abstract class Document : IDisposable, IErrorProne // My Template
{
    public void Process()
    {
        //Error Occured
        OnReportError(); // safely triggers error reporting
    }
}

public class Reporting
{
    static Reporting instance = new Reporting();

    public void HandleError(object sender, ErrorEventArgs e);
}

public partial class Form1 
{
    private DocumentCreator docFactory 
                                = new DocumentCreator(new RandomPicking());
    private Document theDoc     = null;
    private Reporting reporting = Reporting.Instance;
    private DocState state      = new InitialState(); 
    //DocState not in this example but demonstrates how it might work

    public Form1()
    {
        docFactory.ReportError += reporting.HandleError;
        theDoc.ReportError     += reporting.HandleError;

        docFactory.ReportError += state.HandleError;
        theDoc.ReportError     += state.HandleError;
    }

    void BackgroundWork(...)
    {
        using (theDoc = DocumentFactory.Instance.CreateDocument())
        {
           if (theDoc != null)
               theDoc.Process();
        }
    }
}

我想我的问题是,如果我有一个多对一,而不是一对多,那么它是一个反模式吗?

I am wondering if the following is bad use of the Observer pattern. I know the Subject is supposed to be the one and the listener the many. However, I could end up with more subjects in my application than listeners!

The Players

Form1: Self explanatory
DocumentCreator: Contains a Factory Method and a Strategy for picking a file from a list
Document: Contains information about the document file and a Template method for children

Proposed

IErrorProne: interface for the above players to implement an event, turning them into subjects
Reporting: listens for IErrorProne objects and handles logging/emailing
DocumentState: This is a bonus that Im a bit iffy on. I havent quite settled on a good flow outside of the template. Currently I have a state machine inside the Document class. I want to pull the state machine out of the Document class and into Form1, decoupling the two from each other.

public interface IErrorProne
{
    public delegate void ErrorEventDelegate(
        object sender, 
        ErrorEventArgs e
        );

    public event ErrorEventDelegate ReportError;
}

public abstract class Document : IDisposable, IErrorProne // My Template
{
    public void Process()
    {
        //Error Occured
        OnReportError(); // safely triggers error reporting
    }
}

public class Reporting
{
    static Reporting instance = new Reporting();

    public void HandleError(object sender, ErrorEventArgs e);
}

public partial class Form1 
{
    private DocumentCreator docFactory 
                                = new DocumentCreator(new RandomPicking());
    private Document theDoc     = null;
    private Reporting reporting = Reporting.Instance;
    private DocState state      = new InitialState(); 
    //DocState not in this example but demonstrates how it might work

    public Form1()
    {
        docFactory.ReportError += reporting.HandleError;
        theDoc.ReportError     += reporting.HandleError;

        docFactory.ReportError += state.HandleError;
        theDoc.ReportError     += state.HandleError;
    }

    void BackgroundWork(...)
    {
        using (theDoc = DocumentFactory.Instance.CreateDocument())
        {
           if (theDoc != null)
               theDoc.Process();
        }
    }
}

I guess my question is it an Anti-Pattern if I have a Many to One, rather than a One to Many?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

随风而去 2024-10-20 10:03:45

如果您将其视为发布-订阅,那么这真的没关系。如果您采用领域事件样式,则可以让任何事物和任意数量的事物发布任何给定的领域事件,并且让任何事物和任意数量的事物订阅领域事件。

多->多、多->一、一->多都是有效的。

If you think of it as publish-subscribe, then it really doesn't matter. If you take the Domain Event style, you can have anything and any number of things publish any given domain event, and anything and any number of things subscribe to domain events.

Many->Many, many->one, one->many are all valid.

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