Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What design pattern shall I use in this question?

To be frank, this is a homework question, so I'll tell you my opinion. Can you let me know my mistakes rather than giving me the solution?

This is the question :

Assume a restaurant that only offers the following two types of meals: (a) a full meal and (b)an economic meal. The full meal consists of the following food items and is served in the following order: 1. Appetizer 2. Drink 3. Main dish 4. Dessert Meanwhile the economic meal consists of the following food items and is served in the following order: 1. Drink 2. Main dish

Identify the most appropriate design pattern that can be used to allow a customer to only order using one of the two types of meals provided and that the meal components must be served in the given order.

I'm confused between the Factory and the Iterator and using them both together. Using the factory Pattern we can create the two meals full and economic and provide the user with with a base object class that will decide upon. But how can we enforce the ordering of the elements, I thought of using the iterator along that will iterate through the the composite of the two created factories sort of speak.

What do you think?

like image 697
Iyad Al aqel Avatar asked Jan 09 '11 12:01

Iyad Al aqel


2 Answers

First thing that comes to mind is the Decorator pattern That way you could create a Meal base and 2 concreate meals FullMeal and EconomicMeal then you can have the components of the meal as decorators and mix and match them as you like.

like image 185
JHurrah Avatar answered Oct 17 '22 19:10

JHurrah


It's a two step process, which is I think where you're getting confused. The core thing they're looking for is the pattern you will use to select the logic that a user will order their meal with. What that logic actually does, or what order the meal is served in isn't relevant to that piece.

So you'd have a base Meal interface or abstract class that has a method or methods for placing an order (it doesn't say that the food has to be requested in the order of serving it, either, you'll note). That Meal class will probably have a couple methods, one of which includes ServeFood() or similar. You'll have two concrete classes for that (e.g. FullMeal and EconomyMeal), and since the order of them is unimportant, you can implement using a Factory.

The concrete classes will be responsible for serving the food in the correct order when ServeFood() is called on them.

like image 20
Paul Avatar answered Oct 17 '22 20:10

Paul