Ex09: Decorator vs. Visitor

CMYK
Neuling
Neuling
Beiträge: 3
Registriert: 30. Apr 2013 18:47

Ex09: Decorator vs. Visitor

Beitrag von CMYK »

I don't understand, how the decorator pattern and the visitor pattern can coexist in this task.
Either we decorate our objects, so we can ask the outer decorating object to return the accumulated price, calories and the name, then there is no need for a visitor to traverse my objects, he will just visit the outer decorating objects.
Or we implement a visitor pattern, which traverses all objects that build up the pizza to sum up the price, name and calories. In this implementation, the decorator pattern shrank to a linked list.
We decided to implement the first variant since the task description does not indicate which approach weights more.

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: Ex09: Decorator vs. Visitor

Beitrag von imaier »

It's not really an either-or question, of course they can coexist. While there is only one way to calculate calories and prices (if you really want to, we can argue about the latter but not about the former), there is no one single true receipt format. For example, there are many ways how to print toppings on the receipt. You can vary the formatting, but you could also abbreviate names or just print "(+extras)" or "(no extras)", for example. One could also think of printing two receipts with different formatting, one for the customer and one for your archives/accounting.

Therefore, it makes sense to use a visitor for receipts but have price and kcal calculations built into the decorators. Do not put everything into the name string, but imagine instead that you'd have another visitor which prints toppings differently. We did not ask you to implement two visitors, because I believe the task as it is is big enough for a homework.

You also say that in your scenario "the decorator pattern shrank to a linked list". In fact, the decorator pattern will always create a linked list: the outer object is the head of the list, the element it decorates is the second elements and so forth.

Cheers,
Ingo

burgi
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 115
Registriert: 15. Apr 2009 18:08
Wohnort: Ludwigshafen

Re: Ex09: Decorator vs. Visitor

Beitrag von burgi »

Hi,

just to get your reply right Ingo. The idea of this exercise is not to use a "static decorator" like the one described in slideset 5.3 slide 24, right ?

Greetings Dominic

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: Ex09: Decorator vs. Visitor

Beitrag von imaier »

I wasn't aware that mixins are called static Decorators on the slides. Thanks for the pointer. So no, please use the traditional Gamma/GoF style decorator, i.e., the one you can also implement in Java using explicit forwarding (or delegation).

That being said, mixins are of course a viable alternative, but please don't use them here, you already used in them in one of the earlier exercises about widgets/GUI.

As a bit of a side note and an additional learning effect, you could think about whether delegation is necessary here or how the system would need to change so that you could benefit from delegation over forwarding and ultimately, and how mixins are related to forwarding and delegation. For the last part you'd need to know how a bit about how classes in Java and traits in Scala are implemented.

Cheers,
Ingo

burgi
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 115
Registriert: 15. Apr 2009 18:08
Wohnort: Ludwigshafen

Re: Ex09: Decorator vs. Visitor

Beitrag von burgi »

Hi,

thanks a lot for the last reply Ingo, that helped me quite a bit.
But now I have to reask about the price and caloris calculation. Assuming the accumulated prices and calories are stored within the decorators as mentioned above, I have quite a problem with the printing of the prices.
For example I have a Pizza salami with Cheese decoration. Thus I have a concrete decorator of type Cheese, with the accumulated price and calories for pizza salami with additional cheese in it, also a refrence to the concrete element of type PizzaSalami (with price and calories for Pizza salami stored within). Thus when printing the bill I have to print all the subtotals, which is quite annoying for me. The result will look like this:

Pizza: Salami Pizza Calories: 1160.0 Price: EUR 5.99
+Topping: Cheese Calories: 1252.0 Price: EUR 6.68
+Topping: Ham Calories: 1287.0 Price: EUR 7.67

I dont see a way to get a result like:

Pizza: Salami Pizza Calories: 1287.0 Price: EUR 7.67
+Topping: Cheese
+Topping: Ham

So I'm totally wrong if thinking this is not the right way I'm doing this at the moment, or is the first result of the billprinter okay in your opinion ?

Greetings Dominic

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: Ex09: Decorator vs. Visitor

Beitrag von imaier »

No, the first one is not a good format for obvious reasons. In order to avoid printing all subtotals you have to find a way to get the price and calories from the fully decorated pizza plus visit all its toppings. In order to do so, you have to distinguish between visiting a (topped) pizza and visiting its toppings.

I hope this helps,
Ingo

burgi
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 115
Registriert: 15. Apr 2009 18:08
Wohnort: Ludwigshafen

Re: Ex09: Decorator vs. Visitor

Beitrag von burgi »

Hi,

so just to get it right, I'm not totally wrong when storing the subtotals within the decorating objects and not within the decorated object ? For example The subtotal for price ans calories for a family sized pizza salami is stored within the FamilySize class and not within the SalamiPizza class,

Greetings Dominic

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: Ex09: Decorator vs. Visitor

Beitrag von imaier »

Please think about what you asked again. I am sure that you can answer it on your own.

If not, read on...

You can answer this question from multiple angles:

1.) The sheet says use immutable objects. Storing total prices in decorated objects is hence not possible, since at least at the point of their creation, an object does not know how it will be decorated.
2.) Since we have immutable objects, we can decorate the same object multiple times. A consequence of this is that a pizza object does not really represent a single unique real-world instance of a certain pizza, but rather a *description* of multiple instances in the real-world. Another consequence is that we'd need to store multiple prices per pizza if we'd store them in the decorated objects.
3.) Intuitively: a pizza object represents a description of a real-world pizza with a certain price. Decorating pizza P with a topping does not change the price of pizza P, since by decorating it, you obtained a different pizza.

Cheers,
Ingo

burgi
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 115
Registriert: 15. Apr 2009 18:08
Wohnort: Ludwigshafen

Re: Ex09: Decorator vs. Visitor

Beitrag von burgi »

Okay thanks,

so I got that point right so far, I was just somewhat confused with the traversal logic

Greetings

Antworten

Zurück zu „Archiv“