mardi 27 septembre 2022

What pattern should be used, strategy?

I do have a service which needs to handle two types of meal.

@Service
class MealService {

   private final List<MealStrategy> strategies;

   MealService(…) {
       this.strategies = strategies;
   }

   void handle() {
       var foo = …;
       var bar = …;
       strategies.forEach(s -> s.remove(foo, bar));
   }
}

There are two strategies, ‘BurgerStrategy’ and ‘PastaStrategy’. Both implements Strategy interface with one method called remove which takes two parameters.

BurgerStrategy class retrieves meals of enum type burger from the database and iterate over them and perform some operations. Similar stuff does the PastaStrategy.

The question is, does it make sense to call it Strategy and implement it this way or not?

Also, how to handle duplications of the code in those two services, let’s say both share the same private methods. Does it make sense to create a Helper class or something?

Aucun commentaire:

Enregistrer un commentaire