lundi 15 juin 2020

Best practice to create factory where there is some differences in implementation classes

I have the following Interface

public interface CityValidator {
    public vaalidateChecksum(String cityCode); 
    public vaalidateChecksum(String cityCode, String postIndex); 
}

And I have two different implementations In one of them I only need vaalidateChecksum(String cityCode) and don't need vaalidateChecksum(String cityCode, String postIndex) at all, in other, I need vise versa.

public class KyivCityValidator implements CityValidator{

    public vaalidateChecksum(String cityCode) {
         validateCheeksum(cityCode, null)
    }

    public vaalidateChecksum(String cityCode, String postIndex) {
         // some code
    } 

}

public class LondonCityValidator implements CityValidator{

    public vaalidateChecksum(String cityCode) {
        // I don't need implementation of this method at all
    }
    public vaalidateChecksum(String cityCode, String postIndex) { 
     // some code
    }
}

Then I want to have a factory that returns me Validator and I can use one of the above-mentioned methods that I want. Example:

CityValidator kyivValidator = CityValidatorFactory.getValidator(new Kyiv());
kyivValidator.vaalidateChecksum(cityCode);

CityValidator londonValidator = CityValidatorFactory.getValidator(new London());
londonValidator.vaalidateChecksum(cityCode, postalIndex);

But what is the best case for implementation of

 public vaalidateChecksum(String cityCode) {
    // I don't need implementation of this method at all
 }

in LondonCityValidator class in the case when I don't need it at all.

I thought about just throwing a UnsopportedMethodException into them but I am not sure that is a good decision.

What is the best solution to this issue?

Aucun commentaire:

Enregistrer un commentaire