Then write an item tesing class, which will look similar to what you have already. But you only test the item class/objects here. Leave out the grocery class for the time being. You will write this sort of thing:-I think that as long as no awkward so‑and‑so tries to change the quantity of your apples to a ridiculous amount, it will work all right. In fact, I have only looked at the item class, but it looks good so far4 packets of cookies @$2.30 cost $9.20
Larry Frissell wrote:The method above would give you a compile error since "list" is a simple array of GroceryItemOrder
Junilu Lacar wrote:This may seem a little advance for beginners but I have found that method chaining is a great technique for writing more expressive code.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:but there are many altenatives
Junilu Lacar wrote:Of course, I still prefer the way I did it
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:And how about:
Item.named("apples").count(5).each(0.40).addTo(myList);
Junilu Lacar wrote:The thing about this one is that it increases coupling. Now Item knows about a specific type of object that collects it whereas before there was only the collection type naturally knowing about the type of object it holds. Plus, this doesn't read as naturally as the other way.
"Hey, Item class, make one of yourself named 'apples' that has a count of 5 and a price of 0.40, then have it add itself to myList"
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
@Laura - This is just one of the drawbacks of fluent interfaces: There are many ways to implement them, so you have to be very careful to be consistent and document them well. The best can be very nice to use; but bad ones can be awful.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |