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.
Isn't it essentially a Command Processor?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With