Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to break up responsibility? 2

Status
Not open for further replies.

zooxmusic

Programmer
Nov 24, 2004
96
US
Ok, Here's one that is killing me and I can't figure out the best way to do this. Lets say I have a "Food" that has its main macronutrients i.e. protein, carbs, fat. But these amounts are based off of a certain portion size. If I had to be able to adjust the "size" of the portion and rescale the amount of all the macronutrients for a specific meal, how would I model the "Food". I was thinking something like always store the food in its serving size and then have a "Rescale" object that would take in the unit and size and rescale to a new unit and size. Does that sound correct? Or is it better to store the food at the lowest common denominator i.e. 1 oz of this is these macronutrients and then it is easier to rescale. But thats not true because I could have cup/oz conversions that hold true and then there is the whole metric system. Argh!!!


Food
-----
name:String
servingSize:double (1, 0.5, etc...)
unit:int (oz, cup, etc...)
NutritionalInformation


NutritionalInformation
----------------------
protein:double
carbs:double
fat:double


Rescaler
--------

--------
rescale(fromSize, fromUnit, toSize, toUnit)


Spend like you don't need the money,
love like you've never been hurt and dance like no one's watching!
 
Hi Zoo,
I rewrote your problem in a simplified manner:


********************************************************
Facts:
a. Food has macronutrients [ i.e. protein, carbs, fat. ]
b. Macronutrients based on portion size.

Requirements:
a. Adjust the portion size --> updates macronutrients
********************************************************

I find modeling easier if I place myself in the shoes of a client side programmer trying to reuse your objects.
So, I imagined how i would use a Food item:



1. IFood aFoodItem = FoodRepository.GetFood(100029);
2. aFoodItem.SetPortion(3.0,Unit.Grams);

Look at the line numbers:

Line 1: -> declares a food item as a IFood interface. We do not care what food it is as long as it implements a common interface

Line 1: -> Makes use of a repository or some similar factory o/r mapper to build and return new foods based on an id.

Line 2: -> SetPortion is a method exposed by food. It takes the new portion size and a Unit of measure implemented as an enumeration (C#), i am pretty sure java has enumerated values??


Anyway here is my thinking, Food is an object that contains NutritionalInformation [hashtable or some other struct]. Food also has some state such as portion sizes, which when changed triggers a reclaculation of nutritional measurements.





 
An interface might be overkill here. I'm not sure there will ever be different food classes, although I am concerned about compositions.

For example, apples, flour, sugar and butter are all foodstuffs, and we can give the class attributes of carbs, protein, fat, isOrganic, isGlutenFree, containsNuts etc. to handle this. But what about apple pie? It's a collection of other foodstuffs in specific amounts, so maybe we need a Composite pattern? That way the nutritional value of the pie can be determined from the base ingredients. getCarbs() can either return the carbs * amount, or iterate over the collection summing the carb values of the components?

This might cut down on the numbers of nutritional values that have to be stored on your database. It also gives you the ability to do a "what can I make with what I have left in the refrigerator" type query.

All this food has made me hungry. Off to the canteen for breakfast...
 
Thanks Edward and steve,
I also am looking to implement "Meals" which is also a Composite I believe. So a getCarbs() function sounds right!

Should it be the Food's responsibility to calculate the macronutrients? In life food itself doesn't have the means to calculate itself, right?

I get frustrated and excited at the same time with this. I need to read more every night?

Does anyone have any suggestions for a type of "workbook"? I need something that gives me an assignment and then also gives me a way to check my answers to see if I am doing it correctly. Maybe a CRC Card workbook or something to get me thinking OO instead of procedural.

Spend like you don't need the money,
love like you've never been hurt and dance like no one's watching!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top