lundi 3 avril 2023

Which design pattern to avoid if/else

I'm developing a code where I need to consume a json message from a queue and mapping to a java class to verify some values field and decide what flow/process i have to do.

if (car.getName.equals("Audi") && car.getDoors >= 2){
   //do some proccess in other class
} 
if (car.getName.equals("Opel") && car.getDoors < 2){
   //do some proccess in other class
}
if (car.getEnginePower > 1500){
   //do some proccess in other class
}

The problem is obvious, i need always to add a new if statement when a new rule in business logic is added.

What is the best behaviour design pattern to avoid this if/else strategy to try not Open–Closed principle.

Aucun commentaire:

Enregistrer un commentaire