jeudi 25 juillet 2019

Chain of Responsibility "Exactly one handler"

Was reviewing this article in Wikipedia about chain-of-responsibility:

https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

It states:

[...] for the chain of responsibility, exactly one of the classes in the chain handles the request.

In the C# example we see the following code in the implementation of the ILogger interface:

    public void Message(string msg, LogLevel severity)
    {
        if ((severity & logMask) != 0) //True only if any of the logMask bits are set in severity
        {
            WriteMessage(msg);
        }
        if (next != null) 
        {
            next.Message(msg, severity); 
        }
    }

As you can see if a logger matches it doesn't halt the chain. Instead it allows the next handler to be invoked and so on. Thus if we issue a logging request like so:

   logger.Message("Foo", LogLevel.All);

More than one handlers will be invoked, which in my eyes means that the example given for C# is more like a Decorator pattern (all handlers invoked) instead of a Chain-of-Responsibility (exactly one handler at most). Am I missing something?

Aucun commentaire:

Enregistrer un commentaire