开发者

Is this an Observer Anti-Pattern? (with bonus state machine question)

开发者 https://www.devze.com 2023-02-06 05:01 出处:网络
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

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消