samedi 2 octobre 2021

How can we associate code lines with a variable and then run all the code lines specific to a variable , hence reducing code repetition?

Given I have a set of code lines that run for different scenarios. For example


[A,B,C] // code line 1 runs for scenario A B C

[B,C] // code line 2 runs for scenario B C

I don't want to use if else statements . I want to ask if there is any possible way in java any code patterns or any way we could achieve the following : -

fun (param(any of A,B,C)) {
 [A,B,C] - // code line 1
 [B,C] -  // code line 2
}

I use a param and on the basis of that param , lines associated with that param runs . For eg if I pass A i want only code line 1 to be executed and if i passs B i want both the code lines to be executed.

Aucun commentaire:

Enregistrer un commentaire