Take the 2-minute tour ×

I have a list of algorithms that I want to run on a dataset. For example, say my dataset is a list of addresses. I need to check the validity of the addresses but I have several different algorithms for validating. Say I have validation_one and validation_two. But in the future I will need to add validation_three, validation_four, etc. I need ALL of the validations to run on the address list, even the new ones when they get added.

Is there a design pattern that fits into this? I know strategy is for selecting an algorithm but I specifically need a way to apply all the algorithms on the dataset.

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

This sounds like the Chain of Responsibility where all handlers in the chain accept the request and pass it to the next handler.

share|improve this answer
...which is a Composte. See en.wikipedia.org/wiki/Composite_pattern note that the Component interface as an operation() defined. –  Anders Johansen Aug 27 '12 at 8:18
add comment

You have not stated a language.. but assuming it has generics.

Given a DataSet<T>

Assuming also that there is no cross validation required (i.e. each T can be validated entirely by its own data)

Declare a validation Strategy, with a single method.

IValidate<T>{bool validate(T item);} 

validation_one, validation_two…. Will implement this strategy

Have a List<IValidate<T>> which you can add and remove implementations to.

Foreach item in the dataset call each strategy in the list.

It’s then your choice to how you deal with failures.

share|improve this answer
I've done this kind of thing and the only thing I would add to what @yoztastic said is (in production code) to make the validation method a bit more verbose by having it provide additional error details. You typically don't want to know if the dataset failed, you want to get back a list of know which rows failed, and which rule or rules caused the fail, and a human readable message. "The field 'XYZ' was invalid. The error was 'Out of Range'. The reason was 'This field must be greater 0 and less than 365'." Just a suggustion, but build support for this at the start! –  tcarvin Aug 23 '12 at 12:59
Absolutely agree @tcarvin. I would populate a Map<T,List<string>> where list contains human readable explanation of each strategy failure. Any T's not found in the map are valid by their absence. –  Yoztastic Aug 23 '12 at 13:44
It is an implementation detail, but I created an Error collection and passed that as part of the validation signature. On any validation error, the Validate method implementation added an error object that stored these details. To see if the whole operation was validated clean it was as simple as if (errors.Count == 0). Made it easy to present all the error deatils to the user too by dumping the error collection to a grid. –  tcarvin Aug 23 '12 at 14:15
add comment


Which is basically what the two other answers equate to.

The first answer is an implementation of COmposite. The second one is usign Chain of Rersponsibility as a Composite.

share|improve this answer
No, it's not just Composite. Composite is a structural pattern which organizes objects in tree-like structures. There's no notion of "processing" the tree, you need an additional pattern like Visitor to introduce "processing". Your answer is then not complete if it doesn't mention the Visitor. On the other hand, Chain of Resp is a behavioral pattern which is built up around the notion of processing requests by handlers. Regards. –  Wiktor Zychla Aug 24 '12 at 14:47
Composite is absolutely a way to process the tree (or list) of similarily typed items that are in the composite. Why else have a composite at all? Visitor is only used if you want to inject the algorithm in the composite. Composite is usually classified as a structural pattern, but the core concept is that you can make many act like one - structurally and behaviorally. Chain of Resp. mainly differs from Composite in the conditional forwarding and optional deferring handling of the message/request. –  Anders Johansen Aug 27 '12 at 8:13
add comment

Your Answer


