lundi 14 décembre 2015

Design Pattern Selection Chain of Responsibility With Shared Links

I am working on a new tool for my work to control a system. The software should be capable of accepting inputs from a variety of sources, DB monitor, HTTP inputs, other Ethernet Inputs, and who knows what will come, then based on these inputs, signal the system to perform different functions.

So far I have written an interface IInputMonitor which is the base interface for classes that get inputs. IInputMonitor has a member IRequestHandler which is responsible for handling requests that originate at this input monitor. This looks like:

public interface IInputMonitor
{
    bool Start(IRequestHandler handler);

    bool Start();

    bool Stop();

    IRequestHandler RequestHandler
    {
        get;
        set;
    }
}


public interface IRequestHandler
{
    IRequestHandler NextHandler
    {
        get;
    }

    bool HandleRequest(Request request);

    bool Start();

    bool Stop();
}

Most requests will pertain to manipulating the system, but some requests could perform ancillary functions, because of this I originally envisioned the IRequestHandler class implementing a CoR pattern. This way system based requests can be handled by the main system, and other functions can get passed along to a class designed for handling that particular request.

My issue here is that different input monitors can share a single request handler but not not all the handler links in a chain. Strictly following the CoR pattern here would result in something like a tree or long wasteful chains.

To sidestep this I made a special IRequestHandler that functions like a dispatcher, somewhat like the Observer pattern????

    internal class SharedRequestHandler : IRequestHandler
{

    private List<IRequestHandler> sharedHandlers;
    private HashSet<string> sharedHandlersIds;
    private List<IRequestHandler> ownedHandlers;

This class manages lists of handlers and forwards requests through them.

I feel like this is a messy pattern. A handler could be a single CoR chain if its simple, or be a SharedRequestHandler which forwards requests through a chain it its self maintains. Children of the SharedRequestHandler could also be small chain's themselves if they were setup as such. Is it normal to mix & match patterns like this? Is there a better pattern for this sort of system? I want to keep things flexible for future additions, but avoid making the system so complicated its impossible to follow and understand

Aucun commentaire:

Enregistrer un commentaire