mardi 10 mars 2015

Is this the Facade pattern? If not then what?

I am trying to figure out what this design pattern is. I used it often when I have common logic that needs to execute on/for a single object.


I am thinking maybe Facade?


E.g. Validation


A public interface



public interface IValidator<T>
{
void Validate(T obj);
}


Multiple implementations



public class CodeValidator<T> : IValidator<T> { public void Validate(T obj) { } }
public class PerfValidator<T> : IValidator<T> { public void Validate(T obj) { } }
public class DotValidator<T> : IValidator<T> { public void Validate(T obj) { } }
public class NetfValidator<T> : IValidator<T> { public void Validate(T obj) { } }


And then a class takes either a IValidatorCollection or straight IEnumerable<IValidator> which injects all registered validators.



public class PostValidaitonController
{
private IEnumerable<IValidator<Post>> _validators;

public PostValidaitonController(IEnumerable<IValidator<Post>> validators)
{
_validators = validators;
}

public void Validate(Post post)
{
foreach (var validator in _validators)
{
validator.Validate(post);
}
}
}

Aucun commentaire:

Enregistrer un commentaire