Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 952
Jake Aitchison Avatar asked May 28 '26 08:05

Jake Aitchison


2 Answers

Isn't it essentially a Command Processor?

like image 128
David Osborne Avatar answered May 31 '26 06:05

David Osborne


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.

like image 43
jlvaquero Avatar answered May 31 '26 06:05

jlvaquero