I’m trying to create a food ordering application. It will recieve menu data from a webservice (format not yet decided), and turn it into an order form.
I’m having trouble working out how to approach pizzas – they are more complicated than anything else as you can choose the size, base, toppings etc.
I want to have a class of Product, which I can create for each item in the menu. Then, this product will be given objects of class Size and Option. This allows a pizza to be created, its size set (e.g. regular/large/xl) and a set of toppings (Option) to be set.
I need, somehow, for the Option objects to know what the Size of the Product is. I need to do this, as a topping may cost £5 for a regular pizza, but the same topping cost £7 for a large one. Ideally, Option wouldn’t be an element of Size, as the options available stay the same for each size – only the price changes.
My (probably wrong) model looks like this: http://yuml.me/diagram/scruffy/class/%5BPizza%5D-%3E%5BToppings%5D,%20%5BPizza%5D-%3E%5BSize%5D
Any ideas on how I could achieve this?
I would suggest the Decorator pattern as a starting point.
A base Pizza class will have properties and methods for adding sauces, toppings, etc. Decorators can modify the size of the base Pizza. For example, an ExtraLargePizza decorator will change the size. Each decorator should expose the functionality for calculating the cost of the pizza. This is because the decorator knows its own size.