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:

interface IValidator:

public function isValid(rng as range) as boolean
end function

implementation validator_Number:

'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

implementation validator_Capitalized:

'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

validation checker:

'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?

1

isValid() could be a method implemented in a Strategy pattern. IMO, the benefit of the pattern is future modifiability. That is, there will be a future need for new implementations of isValid() and you don’t want to change your code that iterates and validates. In the definition of Strategy, that’s the Context class.

Strategy pattern

Your select block, which I believe would be in the Context class, isn’t flexible the way you proposed it, mainly because it “knows” the concrete implementations:

    select case sheet.cells(headerRow,column).value
        case "NAME"
            set validator=new validator_Capitalized
        case "AGE"
            set validator=new validator_Number
        'etc.
     end select

To make Strategy work the way you want, you probably need a Simple Factory that encapsulates the new XXX. So, instead of a select/case, you just do:

    set validator = Factory.create(sheet.cells(headerRow,column).value)

which effectively calls a create method with the argument “NAME”, “AGE”, etc. which will return the appropriate validator. The create() encapsulates the select/case logic.

The benefit of this is that you can add new validators without having to change the code in Context. Of course, you do have to update the Factory.create() method (essentially the new case for a new validator) and write a new IValidator implementation.

Here’s an updated diagram:

Strategy with simple factory

The factory class as well as the separate implementation classes are a lot of extra code which IMO won’t “pay off” if you only have two validators and a single Context. If you plan on adding lots of validators and really want the flexibility of keeping the details hidden from Context (which may appear in several places), then go for it!

If you aren’t “feeling pain” from repetition in maintaining code, then probably you don’t need a pattern.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật