jeudi 5 novembre 2015

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.

Aucun commentaire:

Enregistrer un commentaire