What design pattern shall I use in this question?
Asked Answered
B

3

7

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?

Blueing answered 9/1, 2011 at 12:37 Comment(5)
I think the drink should be served first in both meals.Sero
Factory and iterator sound good! I don't get what you mean 'iterate through the the composite of the two created factories'. Could you perhaps explain how you would use the iterator to design the full meal (ignoring the factories and economic meal for a second)?Thrasher
assuming each item is a class and get served to the customer by a method in that class , how can i serve " apptizer then drink , ..etc " should i use iterator to iterate through those objects ?Blueing
It's a pretty dumb question. Who is asking you this?Geronto
^^^ LOL the doctor , its an assignment as i mentioned before :SBlueing
F
1

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.

Footstone answered 9/1, 2011 at 15:22 Comment(1)
I thought about decorator pattern, too, and in a real-world app I'd probably use it, but it's really built to do ad-hoc feature addition, not so much for enforcing a particular workflow / mix of features which this problem calls for.Antho
A
1

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.

Antho answered 9/1, 2011 at 13:57 Comment(3)
As a follow up, if you're also supposed to talk about how to serve the food, then inside the concrete classes, you'd probably do an iterator.Antho
that's exactly what i was trying to say , but you stated it in a magical way.. thank you for making the idea much cleared now :)Blueing
If you want a fun way to really grok design patterns, iyad, you should check out Head First Design Patterns (amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/…). While the Gang of Four is the seminal work, the Head First approach is really accessible and fun for getting started on some of the most common ones.Antho
F
1

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.

Footstone answered 9/1, 2011 at 15:22 Comment(1)
I thought about decorator pattern, too, and in a real-world app I'd probably use it, but it's really built to do ad-hoc feature addition, not so much for enforcing a particular workflow / mix of features which this problem calls for.Antho
Q
-4

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.

Quadriplegia answered 9/1, 2011 at 13:48 Comment(5)
You are missing the point. This homework. In other words a fake scenario has been developed to aid in learning when to apply design patterns.Woodcut
@Finglas: I dare say that you are missing my point. I understand perfectly that this is a homework, and what the homework is. I am saying that this homework (as cited) does not define the programming/design problem that needs to be addressed using design pattern. Or that it is too implicitQuadriplegia
Assuming its addressed , what's wrong with my solution guys ?!Blueing
@iyad al aqel: your solution seems fine - each meal type will implement an iterator i/f that will allow iterating thru the dishes from first to last, and there will be a factory method (e.g. Meal::getMeal(mealType) )that will create a requested concrete meal type. So each concrete Meal would implement 2 interfaces: Meal (with nothing to do in your case) and MealIterator, that would return a Dish when dereferenced.Quadriplegia
With a mind like that, I don't know how you managed to get through school in the first place. Certainly if you live in the United States, you've learned that formalized education is about contrived examples and most of the time has surprisingly little to do with the "real world". This doesn't deserve a down 1, but it probably should have been a comment rather than an answer.Sero

© 2022 - 2024 — McMap. All rights reserved.