dimanche 3 octobre 2021

Improve many ifs that returns enumeration with Design Patterns

Of course, when we talk about number of ifs, we try to use the Strategy Pattern. But how about when I want to separate the conditions inside if().

For example, I am developing a game with many possibilites:

I have a class called Game. I check the method and return an enumeration.

if(TieByPoints()) {
  return Result.TieByPoints;
}
if(TieByTimeOver()) {
  return Result.TieByTimeOver;
}
if(JustOnePlayerWin()) {
  return Result.PlayerWin;
}
if(ManyPlayersWin()) {
  return Result.PlayerWin;
}

But many other possibilites/rules can be created. So, is there a way to separate in different classes? Put these conditions in different places?

Aucun commentaire:

Enregistrer un commentaire