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 givi开发者_运维知识库ng 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?
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.
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.
I think that the homework is not well formulated. As cited here, I don't see the challenge.
Identify ... design pattern that can be used to allow a customer to only order using one of the two types of meals
What's the issue? Just present the customer with 2-options choice ("choose meal type (1-full, 2-economy):
" and you're done. Why do one need a design pattern for that?
Design patterns are a tool for the programmer/designer, not the user. So the correct (IMHO) question should be "propose design pattern(s) that would allow the programmer to...", e.g. "... to combine/modify a meal type based on given fixed dish types" or something of the sort.
As stated, the question does not define a programming/design problem, or silently assumes too much.
精彩评论