-1

I have been attending interviews lately and there was one such question related to system design which I could not answer. The scenario is mentioned below :

Scenario : There is a beverage machine which gives beverage like coffee, milk etc. What will be your design pattern/approach with this ?

My Answer : Interface : Beverage machine

Implementing classes : milk, coffee, etc. etc.

Or may be use inheritance to make Beverage machine as parent class and child classes will be milk, coffee, etc.

Now the question is suppose if there are 1000 beverages that the machine provides, will you be writing 1000 implementation classes for that. This was where it made me think for a suitable design. I don't know if he was taking the question towards microservices or not but being a simple java related question it doesn't seemed something related with microservices.

Any help on this will be greatly appreciated. Thanks in advance.

  • 1
    Interface: Dispenser - Contract: `getName`, `getAmount`, `refill`. Interface: DispenserFactory - Contract: `getDispenserByName` Implementation specification: Read from database or filesystem the types of product(s) to dispense. Hope that helps. – Elliott Frisch Aug 21 '21 at 02:39
  • 3
    why does milk, coffee etc need to be classes? They could just be an `Enum` type say `Beverage`. The `BeverageMachine` class could then have a method that makes (returns) these `Beverage` through a method. – Bajal Aug 21 '21 at 02:43
  • 1
    1970s-1990s classical object orientation never panned out. Realistically you'd design your entities based on the required functionality more than the physical objects involved. Are you creating Squarespace for online vending machines and need comprehensive customization? Are you controlling physical hardware and need to control temperatures and nozzles? Is it a vending machine in a video game that just dispenses health potions for coins? You might just have a `class SqliteVendingMachine implements VendingMachine` and deal with a `class Product` containing a product ID and text/image resources. – that other guy Aug 21 '21 at 03:19
  • I think to say that 'OO of the 70's-90's never panned out' is bit out there. Sure it doesn't solve every problem, but then nothing does; sure you can have bad OO implementations, but same goes for any technology. – Adrian K Aug 21 '21 at 06:04

1 Answers1

1

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:

  1. 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.
  2. 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.

Adrian K
  • 9,880
  • 3
  • 33
  • 59
  • ""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."" I designed dropBox best I can and still got rejected because interviewer was not open to discuss at all. – Amit Mar 02 '22 at 05:50