简体   繁体   中英

Iterating through strategy design pattern

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.

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

Given a DataSet<T>

Assuming also that there is no cross validation required (ie 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.

这听起来像责任链,链中的所有处理程序都接受请求并将其传递给下一个处理程序。

Composite.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM