Was...
Here is a beverage machine which gives beverage like coffee, milk etc.
What will be your design pattern/approach with this?
...literally the question? And was it in the context of OO design and development? And you're going for developer roles?
If it was an architect role I'd almost say it was a trick question, because there's not enough information in that statement on which to base a specific answer - therefore it's a trick question intended to see if you are a critical thinker.
Assuming it was couched as an OO question and you've paraphrased the question as asked...
Since beverage is the output, a Factory Pattern might be what they were after.
I must tell you that I only came up with that after more time than you would have in an interview, and I have 20+ years in the industry. So don't be too hard on yourself if you miss something in an interview - everyone does at some point.
Now the question is suppose if there are 1000 beverages that the
machine provides, will you be writing 1000 implementation classes for
that.
Yes, unless you have a class/design/solution that is data-driven. The factory pattern can help with the creation in a sensible way, but at some point you have to code something that represents the beverage, because whilst it can spit out IBeverage objects, those objects have to come from a class at some point.
Not part of your question, but I feel it's worth mentioning... Unfortunately with developer interview situations (in western culture) it can often go one of two ways:
- The interviewing dev's main concerns are that you have some reasonable idea of what you're doing, and that you'll be a good team fit.
- The interviewing dev's treat it as a way to compensate for their own lack of self-confidence / man-hood. In which case they might be after a very specific answer which borders on the slightly more cryptic than is practically necessary.
Hopefully when they asked you that question they weren't in the second camp, because if they were, whilst we can provide answers here which we think are sound, they might not have been exactly what they were after.