Jake Aitchison
Jake Aitchison

Reputation: 1099

What is this design pattern called?

Take a bunch of IProcess implementations find the correct one based on what the implementation CanProcess.

public interface IProcess
{
    bool CanProcess(string name);
    Task Process();
}

public class Processor
{
    private readonly IEnumerable<IProcess> _processors;

    public Processor(IEnumerable<IProcess> processors)
    {
        _processors = processors;
    }

    public void Process(string name)
    {
        Guard.RequireNonNullOrEmpty(name, "name");

        // this could allow for processing multiple matches
        var processor = _processors.FirstOrDefault(r => r.CanProcess(name));
        if (processor!= null)
        {
            processor.Process();
        }
    }
}

Can anyone advise on the name of this pattern, looked at a few but it doesn't seem to fit.

Upvotes: 3

Views: 311

Answers (2)

jlvaquero
jlvaquero

Reputation: 8785

Looks like poor man's chain of responsibility. If instead a Processor you modify IProcess (and its implementations) to allow build up a correlated chain you get the same behaviour plus the the ability to process the same data in various process just in case you need it.

Upvotes: 1

David Osborne
David Osborne

Reputation: 6781

Isn't it essentially a Command Processor?

Upvotes: 5

Related Questions