Henry Wong wrote:
Ryan McClain wrote:
I don't understand what the remove() method is doing in relation to what was stated: how are we moving everything else down by 1 if list[i+1] seems to be moving everything else up by 1?
I recommend using a pencil and paper, draw out an array, picking one of the elements as index i, and follow the for loop in the example (basically, pretend you are the JVM). You will see that the algorithm is indeed moving everything after index i down by 1.
Junilu Lacar wrote:
Ryan McClain wrote:
My arguments to this are:
- Looping through an ArrayList is an incredibly inefficient way to implement this. For every topping, you would have to traverse the ArrayList.
Additionally, ArrayLists are terrible at sorting and searching, inserting an deleting because the underlying algorithms are of O(n) complexity.
- Not only is the loop inefficient, but you are asking the JVM to traverse a complex object graph each time, which adds to the inefficiency.
In the end, this is bad for performance.
How do you know it's really bad for performance? Did you perform benchmark tests? Can you back that up with quantitative data? Or are you optimizing based on what many programmers have been proven over the years to be very bad at: gut feeling and intuition? If you had 15 million different toppings, and customers could add 13 million toppings to an order, then you *might* be justified to worry about performance. But a typical pizza shop has what, 20 different toppings, maybe 40 tops (pun intended)? Is running through a list of 20-40 items really going to tax your computer to the limit of its capacity? I think not. It all smells like premature optimization to me (Donald Knuth calls it the root of all evil).
Now, going back to the video, there were a few things I didn't like about how he implemented his ToppingDecorator and other classes. First, in ToppingDecorator, he called his field "tempPizza". That's lazy. Why "temp"? Bad name.
Conceptually, I don't like the Pizza and Toppings example for Decorator. I always imagine a Decorator as being the same kind of thing as what it is decorating, like in the canonical example of a GUI Window being decorated (wrapped) by a ScrollableWindow which can be wrapped by perhaps a ResizableWindow, etc. I've never liked the Pizza/Topping example, nor the other common Coffee/Additions example. Pizza and the Toppings that you can put on it don't have that "same kind of thing" relationship in my mind. Jalapeno, Cheese, Mushroom, Sausage are not special kinds of Pizza. They are just things I will add on top of a pizza. Same thing for Coffee to cream, sugar, nutmeg, etc.
But why would you do that instead of just making an abstract class topping and defining many different toppings and having an ArrayList of those inside your Pizza. If you want to know the price of the Pizza instance now, you just have to loop through the ArrayList inside your getCost() method and add the price of a plain Pizza to it. Same goes for the description. Just call getName on all the toppings. Besides now you coul have all the toppings subscribe to a sales department or something like that and have it manage their prices.