mercredi 19 mai 2021

What design pattern correspond to described solution

The solution below very similar to the "handlers" concept, but still struggling to find correspondent pattern or (combination of patterns). Instead of words will try to describe through the simple C# code:

interface IHandler
{
    bool CanHandle(string requestType);
    void Handle(object request);
}

IHandler[] handlers = new IHandler[] {h1, h2, ...., hN};

void Handle(string requestType, object request)
{
    foreach(var handler in handlers)
    {
        if(handler.CanHandle(requestType)) 
        {
            handler.Handle(request);

            return;
        }
    }
}

What I want to emphasize here: request object passed to only one or zero handlers. Doing that way I want to make sure that other handlers will not corrupt the state of request.

PS. Another option is to refactor that code into a more pattern-oriented solution.

Aucun commentaire:

Enregistrer un commentaire