Saeed
Saeed

Reputation: 195

Decouple command from its handler

enter image description hereMy command and handler is in two different layers. I want to my command be as a POCO and know nothing about its handler. The solution that came in my mind is something like this:

public interface ICommand
{
    string GetHandler();
}

public interface ICommandHandler
{
    void HandleCommand(ICommand command);
}

public class XCommand : ICommand
{
    //...
    public string GetHandler()
    {
        return "xh";
    }
}

[Handler("xh")]
public class XCommandHandler : ICommandHandler
{
    public void HandleCommand(ICommand command)
    {
        //handle
    }
}

Is this a command pattern?

Upvotes: 4

Views: 184

Answers (1)

jaco0646
jaco0646

Reputation: 17144

No, the Command Pattern encapsulates "handler" logic into the command object itself, resulting in a black-box that can be executed anywhere. In other words, there is no handler role in the Command Pattern. Its concern is when and how a command is executed, rather than who is performing the execution.

However, there are several other behavioral patterns to determine who handles a request or event. Notably, the Chain-of-Command and Observer patterns fit this requirement.

Upvotes: 2

Related Questions