mardi 27 juillet 2021

refactoring if else to strategy design pattern

I want to refactor below if else based logic to use strategy pattern but as per my understanding strategy pattern comes into hand when we want to choose different type at run time but in my case, type(carService) is same but want to invoke different method on the same type based on carType enum. If additional car types added in future then this if else condition will become complex.

Can some one share some example how to refactor below logic to strategy pattern.

    if (CarType.PREMIUM.name().equals(carType)) {
            carService.getPremiumCarDetails();
    } else if (CarType.NORMAL.name().equals(carType)) {
            carService.getNormalCarDetails();
    }

Aucun commentaire:

Enregistrer un commentaire