I want to offer recipes to my users, so I get recipes from the source JSON(including their ingredients).
Currently, the ingredients can be extracted in three ways:
3 tomatoes (without a specific unit)125ml of milk (volume unit, metric or imperial)500g of pasta (unit of mass, metric or imperial)
Requirements
I want to use the DDD approach, so layered architecture.
I need to be able to display the ingredient as is, as suggested in my marker list above. The user can choose between metric or imperial representation.
3 tomatoes125ml of milk or 1/2 cup of milk55g of pasta or 2 ounces of pasta
My challenge
I'm not sure how to create a class in order to respect encapsulation and provide an easy-to-maintain design.
, Unit, Ingredient . . IngredientPresenter :
public String present(Ingredient ingredient) {
if ( ingredient.isUnitless() )
return ingredient.getQuantity() + " " + ingredient.getName();
else
return ingredient.getUnit() + " " + ingredient.getName();
}
, units, IngredientPresenter ( ).
, . , , , . , . IngredientPresenter :
public String present(Ingredient ingredient) {
if ( ingredient instanceof UnitlessIngredient ) {
UnitlessIngredient actualIngredient = (UnitlessIngredient) ingredient;
return actualIngredient.getQuantity() + " " + actualIngredient.getName();
} else {
WithUnitIngredient actualIngredient = (WithUnitIngredient) ingredient;
return actualIngredient.getUnit() + " " + actualIngredient.getName();
}
}
, , , , , .
!
. . , ( ) -. , . .