jeudi 26 mars 2020

Modified Composite pattern / Food, Recipes and Subrecipes

I'm having troubles implementing the Composite Pattern correctly (to make it the most efficient).

There are two entities involved: Food and Recipes. They are to be parsed from a CSV file. A Basic Food object would contain a letter (to denote if it's a food or recipe - in this case f), name, calories, fat, carb and protein. The Recipe would contain the letter r and the name of the recipe.

I'm aware that the basic food should be a corresponding leaf, and the recipe would be composite. However, the recipes in the CSV file can contain subrecipes in a way that it has a name (of food) and count (number of servings) pairs. However, the food can be a subrecipe, which raises to question how to make an optimal solution?

My first though is to make the composite hold a List<Food> to store it's composites, and have a property Map<Food, Double>. What would be the easiest way of checking if we're dealing of a certain type of food and provide CRUD-like functionalities? The program implementation is in Java.

E.g. b, FoodName, 1, 2, 3, 4 (basic food) 
E.g. r, RecipeName, foodOneName, foodOneCount, foodTwoName, foodTwoCount, ...

Aucun commentaire:

Enregistrer un commentaire