I'm writing a postcode validation library, so that I can call a helper method
var result = Postcode.IsValid(postcode, country)
To that end I need to have 'classes' that represent supported countries and know how to validate each. At present I have an interface thus:
public interface IPostcode {
bool IsValid(string postcode);
}
and I have to have classes for each country, e.g.
public class USA : IPostcode {
public bool IsValid(string postcode) {
// ... validate here
}
The helper method selects the relevant IPostcode
based on the country code.
The issue is that it feels wrong to have to instantiate classes that have no state or properties, just methods which would be far better if they were static
. But of course static classes can't have interfaces. Is there a better pattern for this?