I'm setting up a data validator that will iterate through the rows of a spreadsheet, and for each column, perform a validation check. I was thinking that this might be an appropriate task for the Strategy Pattern as follows:
public function isValid(rng as range) as boolean end function
'checks whether or not value is a number implements IValidator public function IValidator_isValid(rng as range) as boolean IValidator_isValid=isnumeric(rng.value) end function
'checks whether all letters are capitalized in value implements IValidator public function IValidator_isValid(rng as range) as boolean IValidator_isValid=isnumeric(rng.value) and (ucase(rng.value)=rng.value) end function
'iterates through worksheet cells, validating them and public sub validationChecker() dim headerRow as long dim validator as IValidator headerRow=1 for row=2 to maxRow for column=1 to maxColumn 'check the column header name and instantiate 'validator using the appropriate implementation select case sheet.cells(headerRow,column).value case "NAME" set validator=new validator_Capitalized case "AGE" set validator=new validator_Number 'etc. end select if validator.isValid(sheet.cells(row,column)) then doSomething() endif next next
I previously asked another question about using Strategy to validate Excel data, and the response was that I didn't need to do that because there were only a few types of tests and so the rules could be held in a data store. But here there are going to be many different kinds of validation tests, some of which have several lines of code. Is the Strategy implementation that I outlined above the most appropriate pattern to use here?